The Coupon-Collector Problem
Collecting the last few missing coupons dominates the total time, because each new coupon gets harder to find as the collection fills up — the expected total scales as n times the harmonic number of n.
Prerequisites: How to Attack a Brainteaser
Cereal boxes each contain one of equally likely collectible toys, chosen independently at random each time. How many boxes, on average, do you need to buy before you have all toys? The intuition "about boxes" is badly wrong — the real answer grows noticeably faster, and the reason why is the useful part.
Setting it up with linearity of expectation
Break the collection into stages. Say you're in "stage " for as long as you have exactly distinct toys and are waiting for a new one (a toy you don't already have). Stage 1 runs from the start until your first toy. Stage is the wait for the very last missing toy.
Each stage is a geometric wait. In stage , you already have of the toys, so each new box is a "success" (a toy you don't have) with probability . The expected number of boxes to wait for one success, when each trial succeeds with probability , is . So the expected length of stage is .
Add up the stages. Total expected boxes , where is the -th harmonic number.
Worked example
With distinct toys, how many boxes do you expect to need to complete the set?
. Expected boxes . Completing a set of 50 needs roughly 224 boxes on average — more than four times the naive guess of 50 — because the last few missing toys are each individually rare events once most of the set is already collected.
Total expected time equals the sum of over all stages, which is . The harmonic sum grows slowly, but multiplied by it means completing the last item costs roughly as much as boxes by itself — the tail dominates, not the start.
When an interviewer asks "expected time until every one of things has happened at least once (with equal, independent probability each trial)," reach straight for instead of simulating — it's a fixed formula, not a case-by-case puzzle.
The same tail-dominated shape shows up whenever a strategy needs to "see everything at least once" — testing every code path, sampling every rare regime in a backtest, or a market maker needing every symbol in a universe to trade at least once before a model is fully calibrated. The last few cases are always disproportionately expensive.
Related concepts
Practice in interviews
Further reading
- Motwani & Raghavan, Randomized Algorithms (ch. 3)