~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Probability and expected value
Linearity of expectation, conditioning, Markov-chain equations; answers as fractions.
Main path: 7 problems, about 2 hours.
Notes
Tools
- Linearity of expectation: E[X + Y] = E[X] + E[Y] even when X and Y are dependent. Use indicator variables for "expected number of X".
- Geometric distribution: with success probability p each try, the expected number of tries is 1/p.
- Conditioning / first-step analysis: write E[state] in terms of the next states and solve the linear equations. For HH, E0 = 1 + ½E1 + ½E0 and E1 = 1 + ½·0 + ½E0, which gives 6 (HT takes 4).
- Symmetry: often gives the answer with no computation at all.
Classics
- Coupon collector: n·H(n).
- Gambler's ruin with a fair coin, starting at i and aiming for N: P(win) = i/N.
- Expected rolls until a 6: 6.
- Expected maximum of two dice: 161/36.
- Birthday problem: about 23 people for a 50% chance.
In code: use fractions.Fraction for exact answers, and a quick Monte Carlo to sanity-check.
10 problems
Advanced & competitive
Randomness Random sampling, shuffles and expected value.
Probability and expected value
- Distribution of a dice sum basics py · c++ · java easy
- Surprise scoops: expected flavours tried py · c++ · java easy
- Coupon collector: rolls until every face appears py · c++ · java easy
- Expected maximum of k dice py · c++ · java easy
- Gambler's ruin: chance to reach the target py · c++ · java easy
- Uniform 1..10 from a 1..7 die py · c++ · java medium
- New 21 Game py · c++ · java medium
More practice: 3 problems
- Expected coin flips until a pattern appears py · c++ · java medium
- Optimal execution with a broker backstop Optiver py · c++ · java hard
- Restart strategies for a random solve time assessment 3 levels OpenAI py · c++ · java hard