Skip to content

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 kkth character, replace the current pick with it with probability 1/k1/k. A character at position kk survives to the end of an nn-character file with probability 1k⋅kk+1⋯n−1n=1n\tfrac1k\cdot\tfrac{k}{k+1}\cdots\tfrac{n-1}{n}=\tfrac1n. This uses O(1)O(1) 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 solution

Lessons behind this question

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