Data Structures & AlgorithmsHardGoogle · Flipkart interview

Reservoir sampling keeps k = 2 items from a stream of n = 10 items: the first 2 fill the reservoir, and item i (i > 2) is admitted with probability 2/i, replacing a uniformly random slot. What is the probability that item 4 is in the final sample?

  1. A. 1/10
  2. B. 1/5
  3. C. 1/2
  4. D. 2/9

Correct answer: B. 1/5

Item 4 is admitted with probability 2/4 and then survives each later item j with probability 1 − (2/j)(1/2) = (j − 1)/j. The product telescopes: (2/4) × (4/5)(5/6)…(9/10) = 2/10 = 1/5, the same k/n as every item.

More Coding Interview Problems questions

Exams that ask this topic