Probability notebook
Twelve classic interview problems. Each is solved on paper first, then checked by a 400,000-trial simulation in Python. Press a button to re-run any simulation live in your browser.
Waiting for a six
How many rolls of a fair die do you expect before the first six?
Geometric with p = 1/6, so the mean is 1/p.
HH versus HT
Flip a fair coin until you see HH. What's the expected number of flips? And for HT?
First-step analysis: E[HH] = 6 because a miss after H sends you back to the start; E[HT] = 4 because after H you only wait for a T. Same probability per pair, different waiting times.
Collecting every face
How many rolls until you've seen all six faces?
Coupon collector: sum of geometric waits 6/6 + 6/5 + … + 6/1 = 6·H₆.
Birthday problem
With 23 people, what's the chance two share a birthday?
1 − ∏(365 − k)/365 for k = 0…22.
Broken stick
Break a stick at two uniform random points. What's the chance the three pieces form a triangle?
A triangle needs every piece under 1/2; the valid region is 1/4 of the unit square.
Gambler's ruin
Start with $10, bet $1 at a time with win probability 0.49. What's the chance of reaching $20 before $0?
With r = q/p: (1 − r10)/(1 − r20). A tiny edge against you compounds.
Monty Hall
You pick one of three doors; the host opens a losing door. Should you switch?
Switching wins exactly when your first pick was wrong, which happens 2/3 of the time.
Max of two dice
What's the expected maximum of two fair dice?
P(max ≤ k) = (k/6)², so P(max = k) = (2k − 1)/36 and the mean is 161/36.
Centre in the triangle
Pick three random points on a circle. What's the chance their triangle contains the centre?
The centre is inside unless all three fall in one semicircle, which has probability 3/4.
Optional reroll
Roll a die and win its face value. You may reroll once and must keep the second roll. What's the optimal expected value?
Reroll when the first roll is below 3.5, the value of a fresh roll: ½·3.5 + ½·5 = 4.25.
Secret Santa
52 people draw names at random. What's the chance nobody draws their own?
Inclusion–exclusion: Σ(−1)ᵏ/k! → 1/e. The expected number of fixed points is exactly 1 for any n.
Uniforms past one
Add uniform(0,1) numbers until the sum exceeds 1. How many do you expect to need?
P(N > n) = P(sum of n uniforms < 1) = 1/n!, so E[N] = Σ 1/n! = e.
Simulations differ from the exact answers by sampling error only: the standard error at 400,000 trials is about 0.001 for probabilities.