Weighted data batcher with deterministic resume
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Anthropic's coding round and at Microsoft, last asked May 2026. It tests data loading, sampling and checkpointing.
Build a sampler that draws weighted batches from a dataset registry and can resume deterministically from a saved offset, including the edge case where the batch size does not divide the data evenly.
class WeightedBatcher:
def __init__(self, datasets, weights, batch_size, seed=0, max_samples=None, drop_last=False, state=None): ...
def next_batch(self):
"""A list of (dataset name, item), or None when the run is over."""
def state(self):
"""Something JSON-friendly that resumes the exact same stream."""
Clarifying questions
- Are weights sampling probabilities per item (with replacement), or quotas so each batch is split across datasets by weight?
- When a dataset runs out, is it reshuffled and reused, or dropped and the remaining weights renormalized?
- With
drop_last, is a short final batch dropped only atmax_samples, or whenever an epoch ends mid-batch? - What exactly must match after resume: the same items in the same order as a run that never stopped?
- Can
datasetsbe large lazy sequences, so the state can't hold a copy of them?
What a strong answer covers
- All randomness from one seeded generator per stream (for example derived from
seedplus an epoch counter), so the stream is a pure function of seed and position. - State as small numbers: per-dataset cursor and epoch, samples emitted, and the generator state (or enough to regenerate it), not the data itself.
- Per-dataset shuffles regenerated from
(seed, dataset, epoch)on resume instead of stored. - The last partial batch: either returned short or dropped by
drop_last, andmax_samplesending the run exactly. - Tests that compare an uninterrupted run with one stopped and resumed at every batch boundary, uneven batch sizes, a zero weight, and a dataset with one item.
Follow-ups
- Resume onto a different number of data-parallel workers without repeating or skipping items.
- Change the weights mid-run (a curriculum) and still resume deterministically.
- Stream datasets that don't fit in memory and can't be shuffled in full.
- How would you verify in production that no sample was seen twice?
Where it's asked
- Anthropic · Coding · Last asked May 202655 min · Live coding on a realistic, multi-part problem, a different one from the screen · No AI
- Microsoft · Coding · Last asked Mar 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
data loading · sampling · checkpointing
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

