Reservoir sampling and a check that it is uniform
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Perplexity's technical screen or assessment, last asked Nov 2025. It tests sampling, streaming and statistics.
Pick k items uniformly at random from a stream whose length you don't know, reading it once and keeping only k items in memory. Every k-subset of the stream must be equally likely; in particular, every item must end up in the sample with probability k / n. Then write a simulation that checks your sampler really is uniform, and explain how you would judge the result.
Part 1: sample
Unless you agree on a different interface with the interviewer, assume:
-
reservoir_sample(stream: Iterable[T], k: int, rng: random.Random) -> list[T]—kitems chosen uniformly, or the whole stream if it has fewer thankitems. Takes the random generator as an argument so tests can seed it.
Part 2: check it
-
check_uniform(sampler, n: int, k: int, trials: int) -> float— runs the samplertrialstimes on the stream0, 1, ..., n - 1, counts how often each item is picked, and returns a p-value (or a pass/fail) for "every item is picked with probabilityk / n".
Example
reservoir_sample(range(3), 5, rng) -> [0, 1, 2] # fewer than k items
reservoir_sample(range(10), 3, rng) -> 3 distinct items from 0..9
check_uniform(reservoir_sample, n=10, k=3, trials=100_000)
# each item expected 30,000 times; a correct sampler lands within a few
# hundred of it and the chi-square test on 10 counts (9 degrees of freedom)
# passes. A sampler that always keeps the first item fails at once.
Constraints
- The stream can be far larger than memory, and its length is unknown until it ends.
- O(n) time, O(k) extra memory.
Clarifying questions
- Does order inside the sample matter?
- Is
kallowed to exceed the stream length? - Can I call the random generator once per item, or is randomness expensive?
- Must the sample be available at any point mid-stream, or only at the end?
- Weighted sampling, or every item equal?
What a strong answer covers
- Algorithm R: keep the first
kitems; for itemi(0-based,i >= k) drawjuniform in[0, i]and replace slotjifj < k. - The induction proof that every item seen so far is in the reservoir with probability
k / (i + 1). - Off-by-one traps:
randint(0, i)versusrandrange(i), and their effect on uniformity. - The test: a chi-square goodness-of-fit on per-item counts, why per-item counts alone don't prove every subset is equally likely (count pairs or whole subsets for small
n), and why one run with a fixed seed is not a test of randomness. - Choosing
trialsso a real bias of, say, 1% would be detected.
Follow-ups
- Algorithm L: skip ahead with geometric jumps so the cost is O(k log(n / k)) random draws.
- Weighted reservoir sampling (keys
u^(1/w), keep theklargest). - Sample from a stream split across many machines, then combine the samples.
- Sample over a sliding window of the last
Witems.
Where it's asked
- Perplexity · Technical screen or assessment · Last asked Nov 2025Online assessment or live coding (often CoderPad); problems come in parts, each adding a requirement to your code · Assume no AI unless the recruiter says otherwise
What it tests
sampling · streaming · statistics
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
