← Questions

Reservoir sampling and a check that it is uniform

0
PerplexityCodingLast asked Nov 2025

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] — k items chosen uniformly, or the whole stream if it has fewer than k items. 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 sampler trials times on the stream 0, 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 probability k / 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 k allowed 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 k items; for item i (0-based, i >= k) draw j uniform in [0, i] and replace slot j if j < 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) versus randrange(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 trials so 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 the k largest).
  • Sample from a stream split across many machines, then combine the samples.
  • Sample over a sliding window of the last W items.

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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding