Reservoir sampling

To pick one uniformly random element from a stream of unknown length in O(1)O(1) memory, with what probability should the iith item replace the current pick?

Show hints (2)+
  1. You don't know nn in advance, so the rule can only use ii.
  2. Show each element ends with probability 1/n1/n by telescoping.

Answer

Reveal answer →

1/i1/i for the iith item

Create a free account to see the full worked solution and track your progress.

Interview format: Market Maker, Systematic & Data-Driven Trading

Related questions