Prove reservoir sampling correctness
Problem: Reservoir Sampling from a Stream
Design an algorithm to uniformly sample k items from a stream of unknown and potentially massive length N, subject to the following constraints:
- The algorithm must use
O(k)memory. - The algorithm must process the stream in a single pass.
Your solution should address the following parts:
Part (a): Algorithm Design
- Write the algorithm specifically for the case where
k=1. - Generalize this algorithm to handle any
k > 1.
Part (b): Proof of Correctness
Prove by induction that after processing i items from the stream, each item seen so far has a probability of k/i to be present in the reservoir (the collection of k sampled items).
Part (c): Analysis and Extension
- Discuss the time and memory complexity of your generalized algorithm.
- Outline how you would adapt your algorithm to perform weighted sampling, where each item has an associated weight influencing its probability of being selected.
AI interview prep
Core dilemma
Maintaining uniform random sampling from an unbounded, single-pass data stream with strict memory constraints, while rigorously proving its probabilistic correctness.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Prove reservoir sampling correctness
Problem: Reservoir Sampling from a Stream
Design an algorithm to uniformly sample k items from a stream of unknown and potentially massive length N, subject to the following constraints:
- The algorithm must use
O(k)memory. - The algorithm must process the stream in a single pass.
Your solution should address the following parts:
Part (a): Algorithm Design
- Write the algorithm specifically for the case where
k=1. - Generalize this algorithm to handle any
k > 1.
Part (b): Proof of Correctness
Prove by induction that after processing i items from the stream, each item seen so far has a probability of k/i to be present in the reservoir (the collection of k sampled items).
Part (c): Analysis and Extension
- Discuss the time and memory complexity of your generalized algorithm.
- Outline how you would adapt your algorithm to perform weighted sampling, where each item has an associated weight influencing its probability of being selected.