Prove Reservoir Sampling Keeps Every Log Line Equally Likely

Last updated: November 29, 2026

Quick Overview

You need a uniform random sample of k log lines from a stream of unknown length, such as a day of Search frontend logs, in one pass and O(k) memory. Describe reservoir sampling and prove each item ends up in the sample with probability k/n.

Google
Statistics & Math
Machine Learning Engineer
Google
November 29, 2026
Machine Learning Engineer
Technical Screen
Statistics & Math
Medium

0

0

0 solved


You need a uniform random sample of k log lines from a stream of unknown length, such as a day of Search frontend logs, in one pass and O(k) memory. Describe reservoir sampling and prove each item ends up in the sample with probability k/n.

Sampling huge log streams for labeling and debugging is routine at Google, and this question checks both the algorithm and a clean induction proof. Interviewers watch whether you get the replacement probability right and can argue uniformity rigorously.

What the Interviewer Expects
  • State the algorithm with the i-th item kept with probability k/i
  • Prove by induction that each item is in the reservoir with probability k/n
  • Explain why replacement position is uniform
  • Give O(n) time and O(k) memory
  • Discuss distributed or weighted variants
Key Topics to Cover
Reservoir sampling
Induction proof
Streaming algorithms
Uniform sampling
Weighted sampling
How to Approach This
  1. Define your hypotheses (H0 and H1) clearly before performing any test.
  2. Calculate required sample size BEFORE running an experiment, using power analysis.
  3. Remember the Central Limit Theorem: sample means become approximately normal with large n.
  4. Watch for Simpson's paradox. Always segment data by key dimensions.
  5. Distinguish between statistical significance and practical significance.
Possible Follow-up Questions
  • How would you sample uniformly from a stream split across 1,000 machines?
  • How would you do weighted reservoir sampling?
  • How can you skip ahead to avoid generating one random number per item?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Browse Statistics Questions
Sample Answer
Clarify

Items arrive one at a time; nn is unknown until the stream ends; we must output kk items, each subset of size kk equally likely (in particular each item with probability k/nk/n)....

Approach

Keep the first kk items. For the ii-th item with i>ki > k, draw jj uniformly from 1..i1..i; if j≤kj \le k, replace slot jj with the new item....


Submit Your Answer
Markdown supported