Uniform random character from a stream in one pass
Question
A file holds an unknown number of characters. Reading it only once and without knowing its length in advance, how can you select one character so that every character is equally likely to be chosen?
Short answer
Reservoir sampling: when reading the th character, replace the current pick with it with probability . A character at position survives to the end of an -character file with probability . This uses memory and one pass.
Adapted from Zhou, A Practical Guide to Quantitative Finance Interviews, sec. 7.1 Algorithms, 'Random permutation B', p.176; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solutionLessons behind this question
- Counting and Combinatorics
- Continuous Random Variables and Probability Density Functions
- Cumulative Distribution Functions
Start with a free lesson: Normal Distribution is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.
Read it free