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.
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
How to Approach This
- Define your hypotheses (H0 and H1) clearly before performing any test.
- Calculate required sample size BEFORE running an experiment, using power analysis.
- Remember the Central Limit Theorem: sample means become approximately normal with large n.
- Watch for Simpson's paradox. Always segment data by key dimensions.
- 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 QuestionsSample Answer
Clarify
Items arrive one at a time; is unknown until the stream ends; we must output items, each subset of size equally likely (in particular each item with probability )....
Approach
Keep the first items. For the -th item with , draw uniformly from ; if , replace slot with the new item....