Probability Puzzles from Scratch
May 2026
This post is not a puzzle collection. It is a training program.
Quant interviews at firms like Jane Street, Citadel, Hudson River Trading, Two Sigma, SIG, Tower Research, IMC, and Optiver are not testing whether you have memorized the answer to the birthday problem. They are testing whether you can walk into a room, hear a problem you have never seen before, and — in real time, under pressure — find the right angle of attack. The answer matters less than the path you take to get there.
The good news is that the “right angle of attack” almost always falls into one of a small number of recurring techniques. The same moves — reframing, complement counting, indicator variables, first-step analysis, geometric probability, the reflection principle, optimal stopping, permutation cycles, martingales, order statistics, generating functions, conditional expectation — appear again and again, wearing different disguises. If you internalize these moves deeply enough that they become reflexes, you can solve unfamiliar problems in minutes.
The backbone is nine classic puzzles, ordered from warm-up to elite. Each one teaches a named technique through a problem chosen not because it is famous, but because it is the clearest possible demonstration of a specific way of thinking. But nine puzzles are not enough to build real fluency. So each technique is followed by a deep dive — harder variants, extensions, and interview-difficulty sub-puzzles that drill the same move until it becomes a reflex. Between and around the nine puzzles, four standalone sections build the theory of martingales, order statistics, generating functions, and conditional expectation from the ground up, each with its own worked problems. By the end — over 30 puzzles and 13 techniques later — you will have a complete toolkit, a recognition flowchart for pattern-matching new problems on sight, and the instinct for when to reach for each tool.
Contents
The Nine Puzzles
- The Ants on a Stick — Reframing
- The Birthday Problem — Complement Counting
- Fixed Points of a Random Permutation — Indicator Variables & Linearity of Expectation
- The Coupon Collector — Decomposition into Geometric Random Variables
- Gambler’s Ruin — First-Step Analysis
Technique Deep Dives
More Puzzles
- The Broken Stick — Geometric Probability
- Order Statistics from Scratch
- Generating Functions from Scratch
- Conditional Expectation from Scratch
- The Ballot Problem — The Reflection Principle
- The Secretary Problem — Optimal Stopping
- The 100 Prisoners Problem — Permutation Cycles
Reference
1. The Ants on a Stick
100 ants are placed at distinct positions on a one-meter stick. Each ant faces either left or right, chosen independently. All ants begin walking simultaneously at 1 cm/s. When two ants collide, they both instantly reverse direction. When an ant reaches either end of the stick, it falls off.
What is the maximum possible time until all ants have fallen off?
Before reading on: what is your first instinct? Try to estimate the answer.
The wrong approach
The natural reaction is to try to simulate the system. 100 ants means up to \(\binom{100}{2} = 4{,}950\) possible collisions. Each collision changes the direction of two ants, which creates new collisions, which creates more direction changes. The dynamics seem impossibly complex — a cascading chain of interactions where tracking any single ant requires tracking the entire system.
Some people try to reason about specific configurations: “What if all ants face right? Then the rightmost ant falls off first at time \(t_1\), then the next, and so on.” But what if they face alternating directions? The collision patterns become chaotic.
When two ants collide and reverse direction, this is indistinguishable from two ants passing through each other.
Think about it from a bird’s-eye view. Before the collision: something moves left, something moves right. After the collision: something moves left, something moves right. The “ants” are identical — there is no physical difference between “ant A reversed and is now going left” and “ant B was already going left and continued.”
Replace the collision rule (“reverse”) with “pass through.” The two models produce identical observations at every moment: the same set of positions are occupied by ants, moving in the same directions. Only the labels on the ants differ, and the problem doesn’t depend on labels.
In the pass-through model, each ant walks independently in its original direction until it falls off the end. An ant at position \(x\) (measured in cm from the left end) falls off at time:
- \(x\) seconds, if facing left (walks to the left end),
- \(100 - x\) seconds, if facing right (walks to the right end).
The maximum time for a single ant is \(\max(x, 100 - x) \leq 100\). The worst case is an ant at one end facing the opposite end: it walks the entire length. So the maximum over all initial configurations is 100 seconds.
The problem as stated involves complex, coupled interactions. But there exists an equivalent description of the same system where the answer is trivial. The entire difficulty was in the problem’s framing, not in the underlying mathematics.
When to use it: whenever a problem involves interacting particles, symmetric agents, or collisions between identical objects. Ask: “Can I relabel, reinterpret, or re-describe the system to make the interactions vanish?”
Other applications: random walks with colliding particles, load balancing on symmetric servers, matching problems where you can relabel the participants.
Two slips of paper are placed face-down, each with a distinct real number. You pick one at random and see its value. Can you guess whether your number is the larger of the two with probability strictly greater than \(1/2\)?
Intuition says no — you have zero information about the other slip. But Thomas Cover found a beautiful strategy:
Strategy: before looking, draw a random threshold \(T\) from any continuous distribution with full support (e.g., standard normal). If your number \(> T\), guess “larger.” If your number \(< T\), guess “smaller.”
Why it works: let the two numbers be \(a < b\). There are three cases:
• \(T < a\): both numbers exceed \(T\). You guess “larger” regardless of which you drew. You’re right with probability \(1/2\).
• \(T > b\): both numbers are below \(T\). You guess “smaller” regardless. Right with probability \(1/2\).
• \(a < T < b\): the threshold lands between the two numbers. If you drew \(b\), you see \(b > T\) and guess “larger” — correct. If you drew \(a\), you see \(a < T\) and guess “smaller” — correct. You win with probability \(1\).
Since \(\Pr(a < T < b) > 0\) (the distribution has full support), the overall probability of a correct guess is strictly greater than \(1/2\).
Connection to Puzzle 1: both problems exploit the insight that indistinguishable elements collapse complexity. In Puzzle 1, identical ants make collisions equivalent to pass-throughs. In Cover’s strategy, the random threshold creates a “symmetry-breaking” point that extracts information from a seemingly information-free situation.
2. The Birthday Problem
How many people do you need in a room before there is a greater than 50% chance that at least two of them share a birthday? Assume 365 equally likely birthdays and ignore leap years.
Before reading on: give your gut estimate. Most people guess around 180. The actual answer will surprise you.
The wrong approach
The intuitive reasoning goes: “There are 365 possible birthdays. To get a 50% chance of a match, you need about half of them — around 183 people.” This is wrong by a factor of 8.
The error is subtle: this reasoning treats the problem as “what is the chance a specific birthday is shared?” But the problem asks about any pair. With \(n\) people, there are \(\binom{n}{2}\) pairs, and the number of pairs grows quadratically. The collision probability is driven by the number of pairs, not the number of people.
Don’t compute \(\Pr[\text{at least one match}]\) directly. Compute the complement: \(\Pr[\text{no matches at all}]\). The complement is a simple product.
Person 1 can have any birthday. Person 2 must avoid person 1’s birthday: probability \(364/365\). Person 3 must avoid both: probability \(363/365\). Continuing:
$$ \Pr[\text{no match among } n] = \frac{365}{365} \cdot \frac{364}{365} \cdot \frac{363}{365} \cdots \frac{365 - n + 1}{365} = \prod_{k=0}^{n-1}\left(1 - \frac{k}{365}\right). $$Finding the threshold. We want the smallest \(n\) such that this product drops below \(1/2\). Take logarithms:
$$ \ln\Pr[\text{no match}] = \sum_{k=0}^{n-1} \ln\!\left(1 - \frac{k}{365}\right). $$For small \(x\), \(\ln(1 - x) \approx -x\). Since \(k/365\) is small for the values we care about:
$$ \ln\Pr[\text{no match}] \approx -\sum_{k=0}^{n-1} \frac{k}{365} = -\frac{n(n-1)}{2 \cdot 365}. $$Setting this equal to \(\ln(1/2) = -\ln 2\):
$$ \frac{n(n-1)}{2 \cdot 365} \approx \ln 2 \implies n^2 \approx 2 \cdot 365 \cdot \ln 2 \approx 2 \cdot 365 \cdot 0.693 \approx 506. $$ $$ n \approx \sqrt{506} \approx 22.5. $$So \(n = 23\). Let’s verify: \(\Pr[\text{no match among 23}] = \prod_{k=0}^{22}(1 - k/365)\). Computing this product gives \(\approx 0.4927\), so \(\Pr[\text{match}] \approx 0.5073 > 0.5\). \(\checkmark\)
For \(n = 22\): \(\Pr[\text{match}] \approx 0.476 < 0.5\). So 23 is exact.
The birthday problem is a prototype for collision phenomena in computer science (hash collisions, the birthday attack in cryptography). The general formula says: among \(n\) objects drawn uniformly from a set of size \(N\), the probability of a collision exceeds 50% when \(n \approx \sqrt{2N \ln 2} \approx 1.177\sqrt{N}\). For \(N = 365\), that gives \(1.177\sqrt{365} \approx 22.5\).
When the event you want is a disjunction (“at least one match,” “something goes wrong,” “any collision”), its complement is often a conjunction (“every pair is distinct”), which factors into a product of independent terms.
When to use it: whenever you see “at least one,” “any,” or “some.” Flip to “none” and compute the product.
Other applications: probability of at least one success in \(n\) independent trials, the coupon collector (partial), derangement probability, reliability of systems with independent components.
Going Deeper: Conditional Probability at Interview Difficulty
Complement counting handles the “at least one” structure beautifully. But quant interviews push further: once you know something happened, how you learned it changes the answer. The four puzzles below teach the most dangerous trap in probability — the observation process matters as much as the observation itself. Every one of these has fooled professional mathematicians.
2a. The Two Aces
Two cards are drawn from a standard 52-card deck and placed face down.
Part 1: I tell you that at least one of the two cards is an Ace. What is the probability that both cards are Aces?
Part 2: I turn over one card at random and it happens to be an Ace. What is the probability that the other card is also an Ace?
Before reading on: are the two parts asking the same question? Most people think so. They are not.
Part 1: “At least one is an Ace”
This is straight complement counting. We need \(\Pr[\text{both Aces} \mid \text{at least one Ace}]\).
Step 1: Count the relevant events. Two cards are drawn from 52. Total hands: \(\binom{52}{2} = 1326\).
$$ \begin{aligned} \Pr[\text{both Aces}] &= \frac{\binom{4}{2}}{\binom{52}{2}} = \frac{6}{1326}. \\[6pt] \Pr[\text{at least one Ace}] &= 1 - \frac{\binom{48}{2}}{\binom{52}{2}} = 1 - \frac{1128}{1326} = \frac{198}{1326}. \end{aligned} $$Step 2: Apply the definition of conditional probability.
$$ \Pr[\text{both Aces} \mid \text{at least one Ace}] = \frac{\Pr[\text{both Aces}]}{\Pr[\text{at least one Ace}]} = \frac{6}{198} = \frac{1}{33}. $$Part 2: “A random card is turned over and it’s an Ace”
This sounds like the same information — you now know at least one card is an Ace. But the way you learned it is different: a card was chosen at random (not chosen because it’s an Ace) and it happened to be an Ace. This changes the likelihood of the two hypotheses.
Use Bayes’ theorem. The observation “a random card is an Ace” is more likely under the hypothesis “both are Aces” (probability 1) than under “exactly one is an Ace” (probability 1/2, since the random card might pick the non-Ace). This asymmetry shifts the posterior.
Step 1: Define the hypotheses.
- \(H_2\): both cards are Aces. \(\Pr[H_2] = 6/1326\).
- \(H_1\): exactly one card is an Ace. \(\Pr[H_1] = \binom{4}{1}\binom{48}{1}/\binom{52}{2} = 192/1326\).
Step 2: Compute likelihoods. A card is chosen at random and it’s an Ace:
- \(\Pr[\text{random card is Ace} \mid H_2] = 1\) (both are Aces, so either card works).
- \(\Pr[\text{random card is Ace} \mid H_1] = 1/2\) (only one of the two cards is an Ace).
Step 3: Bayes’ theorem.
$$ \begin{aligned} \Pr[H_2 \mid \text{see Ace}] &= \frac{\Pr[\text{see Ace} \mid H_2] \cdot \Pr[H_2]}{\Pr[\text{see Ace} \mid H_2] \cdot \Pr[H_2] + \Pr[\text{see Ace} \mid H_1] \cdot \Pr[H_1]} \\[6pt] &= \frac{1 \cdot 6/1326}{1 \cdot 6/1326 + \tfrac{1}{2} \cdot 192/1326} = \frac{6}{6 + 96} = \frac{6}{102} = \frac{1}{17}. \end{aligned} $$The probability nearly doubles when you change how the information is revealed. The reason: in Part 1, saying “at least one is an Ace” is equally likely whether you have one Ace or two. In Part 2, a random card being an Ace is twice as likely when both are Aces (guaranteed) versus when only one is (50/50). This extra likelihood boosts the posterior for two Aces.
The meta-lesson: in conditional probability, the observation process matters as much as the observation itself. “I know at least one is an Ace” and “I flipped a random card and it’s an Ace” convey different amounts of evidence, even though they feel like the same information.
2b. The Boy or Girl Paradox
A family has two children. Assume each child is equally likely to be a boy or a girl, independently.
Part 1: At least one child is a boy. What is the probability that both children are boys?
Part 2: You meet one of the two children at random and it’s a boy. What is the probability that the other child is also a boy?
Before reading on: this is the Two Aces puzzle with simpler numbers. The structure is identical — but the cleaner sample space makes the logic crystal clear.
The sample space. Four equally likely outcomes: BB, BG, GB, GG.
Part 1: “At least one boy.” This rules out GG. The remaining equally likely outcomes are BB, BG, GB. Only one of the three is BB:
$$ \Pr[\text{both boys} \mid \text{at least one boy}] = \frac{1}{3}. $$Part 2: “A random child is a boy.” Apply Bayes’ theorem. The likelihood of seeing a boy when you pick a random child is:
- From BB: \(\Pr[\text{random child is boy}] = 1\).
- From BG or GB: \(\Pr[\text{random child is boy}] = 1/2\).
- From GG: \(\Pr[\text{random child is boy}] = 0\).
| Information | Two Aces | Boy-Girl |
|---|---|---|
| “At least one is X” | \(1/33\) | \(1/3\) |
| “A random one is X” | \(1/17\) | \(1/2\) |
| Ratio | \(\approx 1.94\times\) | \(1.5\times\) |
The “random reveal” always gives a higher probability because the event “both are X” makes the observation more likely. The boost factor depends on the base rates.
Sub-skill learned: the Boy-Girl paradox is the simplest case of the observation-process distinction. Use it as a mental model whenever you encounter a conditional probability puzzle: ask yourself, “Is this ‘at least one’ or ‘a specific random one’?”
2c. Bertrand’s Box Paradox
Three boxes each contain two coins:
- Box 1: gold, gold
- Box 2: gold, silver
- Box 3: silver, silver
You pick a box uniformly at random, reach in, and draw one coin at random. It’s gold. What is the probability that the other coin in the same box is also gold?
Before reading on: the naive answer is 1/2 (“two boxes have gold, and in one the other coin is gold, in the other it’s silver”). Is this right?
Why the naive answer is wrong
The reasoning “there are two boxes with gold, equally likely, so 1/2” treats the boxes as equally likely after you observe a gold coin. But they are not equally likely — Box 1 is twice as likely to produce a gold coin on a random draw as Box 2.
Don’t condition on boxes — condition on coins. There are 6 coins total, each equally likely to be drawn. Three are gold (two from Box 1, one from Box 2). Of the three gold coins, two have a gold companion. So the probability is \(2/3\), not \(1/2\).
Method 1: Count equally likely cases. Label the six coins: G1a, G1b (Box 1), G2, S2 (Box 2), S3a, S3b (Box 3). Each coin is equally likely to be drawn. You drew a gold coin — that’s one of \(\{G1a, G1b, G2\}\). In two of these three cases (G1a and G1b), the other coin is gold. So:
$$ \Pr[\text{other is gold} \mid \text{drew gold}] = \frac{2}{3}. $$Method 2: Bayes’ theorem.
$$ \begin{aligned} \Pr[\text{Box 1} \mid \text{gold}] &= \frac{\Pr[\text{gold} \mid \text{Box 1}] \cdot \Pr[\text{Box 1}]}{\sum_i \Pr[\text{gold} \mid \text{Box } i] \cdot \Pr[\text{Box } i]} \\[6pt] &= \frac{1 \cdot 1/3}{1 \cdot 1/3 + 1/2 \cdot 1/3 + 0 \cdot 1/3} = \frac{1/3}{1/2} = \frac{2}{3}. \end{aligned} $$The error is forgetting to weight by likelihood. Box 1 and Box 2 both contain gold, but Box 1 is twice as likely to show you a gold coin on a random draw (probability 1 vs 1/2). After conditioning on the observation, Box 1 gets twice the posterior weight. This is the same mechanism that drives the Two Aces puzzle and the Boy-Girl paradox.
Sub-skill learned: when the naive approach gives “two equally likely cases,” check whether they really are equally likely after conditioning. Count the equally likely elementary outcomes, not the categories.
2d. The Monty Hall Problem
A game show has three doors. Behind one is a car; behind the other two are goats. You pick Door 1. The host, who knows where the car is, opens Door 3 to reveal a goat. Should you switch to Door 2 or stick with Door 1?
Before reading on: most people say it doesn’t matter — there are two doors left, so it’s 50/50. This is wrong, and it is wrong for exactly the same reason as the puzzles above.
Why the host’s knowledge matters
The host didn’t open a door at random — the host chose a door that has a goat, knowing where the car is. This constrained choice changes the likelihoods. If the car is behind Door 2, the host is forced to open Door 3. If the car is behind Door 1, the host could have opened either Door 2 or Door 3.
Step 1: Priors. \(\Pr[\text{car at } i] = 1/3\) for each door.
Step 2: Likelihoods. Given you picked Door 1, the host opens Door 3:
- \(\Pr[\text{host opens 3} \mid \text{car at 1}] = 1/2\) (host can open 2 or 3).
- \(\Pr[\text{host opens 3} \mid \text{car at 2}] = 1\) (host must open 3 — can’t open 2).
- \(\Pr[\text{host opens 3} \mid \text{car at 3}] = 0\) (host won’t reveal the car).
Step 3: Bayes’ theorem.
$$ \begin{aligned} \Pr[\text{car at 2} \mid \text{host opens 3}] &= \frac{1 \cdot 1/3}{\tfrac{1}{2} \cdot 1/3 + 1 \cdot 1/3 + 0} = \frac{1/3}{1/2} = \frac{2}{3}. \\[6pt] \Pr[\text{car at 1} \mid \text{host opens 3}] &= \frac{1/2 \cdot 1/3}{1/2} = \frac{1}{3}. \end{aligned} $$Suppose the host opens Door 3 at random (not knowing where the car is), and it happens to be a goat. Should you switch now?
Now all three likelihoods of “host opens Door 3” are equal (\(1/2\) each, since the host picks randomly between Doors 2 and 3). After conditioning on seeing a goat:
$$ \Pr[\text{car at 2} \mid \text{random host opens 3, sees goat}] = \frac{1/2 \cdot 1/3}{1/2 \cdot 1/3 + 1/2 \cdot 1/3 + 0} = \frac{1}{2}. $$Switching doesn’t help — it’s 50/50! This is exactly the Two Aces puzzle in disguise: a knowledgeable reveal (“I’m telling you at least one is an Ace”) gives a different answer than a random reveal (“I flipped a random card and it’s an Ace”).
| Puzzle | Knowledgeable reveal | Random reveal |
|---|---|---|
| Two Aces | \(P = 1/33\) | \(P = 1/17\) |
| Boy-Girl | \(P = 1/3\) | \(P = 1/2\) |
| Bertrand’s Box | — | \(P = 2/3\) |
| Monty Hall | Switch: \(2/3\) | Switch: \(1/2\) |
Every puzzle in this family has the same skeleton: two hypotheses, an observation, and a likelihood that differs between hypotheses. The answer always comes down to Bayes’ theorem and knowing whether the observation was guaranteed (knowledgeable) or lucky (random).
Sub-skill learned: before computing anything, ask: “Was this information revealed because it’s true, or did someone check a random thing and it happened to be true?” The answer determines which Bayes calculation you need.
2e. The Tuesday Boy
I have two children. At least one of them is a boy born on a Tuesday. What is the probability that both children are boys?
Without the “Tuesday” information, this is the Boy-Girl problem (Puzzle 2b): the answer is \(1/3\). Adding “born on a Tuesday” changes the answer. Here’s why.
Step 1: The sample space. Each child is described by (gender, day of birth): \(2 \times 7 = 14\) equally likely types. Two children give \(14 \times 14 = 196\) outcomes.
Step 2: At least one Tuesday-boy. The complementary count is easier. Outcomes with no Tuesday-boy: each child is one of the \(14 - 1 = 13\) non-Tuesday-boy types, giving \(13^2 = 169\). So:
$$ |\text{at least one Tue-boy}| = 196 - 169 = 27. $$Step 3: Both boys AND at least one Tuesday-boy. Each child is one of 7 boy-types (Mon-boy, Tue-boy, ..., Sun-boy). Two boys: \(7^2 = 49\). Both boys but no Tue-boy: \(6^2 = 36\). So:
$$ |\text{both boys, at least one Tue-boy}| = 49 - 36 = 13. $$Step 4: The answer.
$$ \Pr[\text{both boys} \mid \text{at least one Tue-boy}] = \frac{13}{27} \approx 0.481. \quad\square $$Without the day: \(\Pr = 1/3\). With “Tuesday”: \(\Pr = 13/27 \approx 0.481\). Adding a specific detail about the boy pushes the answer toward \(1/2\).
Why? The more specific the description, the less likely both children match it. When the specifier is very rare (birthday = Dec 25, or name = “Xerxes”), it becomes almost certain that only one child is the specified boy, making the problem equivalent to “I picked a specific child and it’s a boy” — which gives \(1/2\).
In the limit: if the specifier has probability \(\varepsilon \to 0\) per child, the answer approaches \(1/2\). If the specifier has probability \(1\) (no information), the answer is \(1/3\). “Tuesday” (\(1/7\)) is in between.
General formula: if each child independently has the specifier with probability \(s\), then
$$ \Pr[\text{both boys} \mid \text{at least one specified boy}] = \frac{2s - s^2}{4s - s^2} = \frac{2 - s}{4 - s}. $$Check: \(s = 1 \Rightarrow 1/3\). \(s = 1/7 \Rightarrow 13/27\). \(s \to 0 \Rightarrow 1/2\). \(\checkmark\)
The Conditional Probability Playbook
Five patterns for every Bayesian conditioning problem you’ll face.
Before touching any numbers, classify the observation process:
“At least one” / knowledgeable reveal: someone who can see all the data tells you a property holds. Condition on the event.
“Random reveal”: a random sample was taken and happened to show something. Condition on the observation process using Bayes.
If you get this distinction wrong, everything that follows will be off by a factor that cannot be fixed.
(1) List the hypotheses \(H_1, H_2, \ldots\) and their priors \(\Pr[H_i]\).
(2) For each hypothesis, compute the likelihood: \(\Pr[\text{observation} \mid H_i]\). This is the step where the observation process matters.
(3) Apply Bayes: \(\Pr[H_i \mid \text{obs}] = \Pr[\text{obs} \mid H_i] \cdot \Pr[H_i] \;/\; \sum_j \Pr[\text{obs} \mid H_j] \cdot \Pr[H_j]\).
The denominator is just a normalizing constant. The numerator is where all the action is.
When the sample space is small enough, skip Bayes and count equally likely elementary outcomes. In Bertrand’s Box: don’t reason about boxes (unequal likelihoods) — reason about the 6 individual coins (all equally likely to be drawn). Of the 3 gold coins, 2 have a gold companion. Done.
This is faster and less error-prone than Bayes when the elementary outcomes are obvious.
After computing, ask: is the posterior higher or lower than the prior? The direction should make intuitive sense. If the observation is more likely under \(H_2\) than \(H_1\), the posterior for \(H_2\) should increase. If your answer goes the wrong way, you have a bug.
| Problem | Trap | Fix |
|---|---|---|
| Two Aces | Confusing “at least one” with “random reveal” | Separate the two questions; compute likelihoods |
| Boy-Girl | Same as above, simpler numbers | Enumerate BB, BG, GB, GG |
| Bertrand’s Box | Treating boxes as equally likely after conditioning | Count coins, not boxes |
| Monty Hall | Ignoring that the host’s choice is constrained | Compute likelihoods for “host opens 3” |
3. Fixed Points of a Random Permutation
A deck of 52 cards, numbered 1 through 52, is shuffled into a uniformly random order. A fixed point is a card that ends up in its original position (card 1 in position 1, card 2 in position 2, etc.).
What is the expected number of fixed points?
Before reading on: is the answer closer to 0, 1, 10, or 26?
The wrong approach
The brute-force approach: compute the probability of exactly \(k\) fixed points for each \(k\), then take the weighted sum. This requires the theory of derangements — permutations with zero fixed points — and inclusion-exclusion over all subsets. For a deck of 52 cards, the sum has \(2^{52}\) terms. This is correct but absurdly over-engineered.
Don’t compute the full distribution. Decompose the count into indicator random variables, one per card, and use linearity of expectation.
Linearity of expectation says \(\E[X + Y] = \E[X] + \E[Y]\), always. It does not require independence. This single fact is the most powerful trick in all of combinatorial probability.
Let’s prove linearity of expectation before using it, since it is the foundation of everything that follows.
That’s it. The proof is one line. It uses nothing but the definition of expectation and the distributive law. The \(X(\omega)\) and \(Y(\omega)\) can be arbitrarily dependent — the algebra doesn’t care. \(\square\)
Define the indicator random variable:
$$ X_i = \begin{cases} 1 & \text{if card } i \text{ is in position } i, \\ 0 & \text{otherwise.}\end{cases} $$The total number of fixed points is \(X = X_1 + X_2 + \cdots + X_{52}\).
By linearity of expectation (no independence needed!):
$$ \E[X] = \E[X_1] + \E[X_2] + \cdots + \E[X_{52}]. $$Now, \(\E[X_i] = \Pr[\text{card } i \text{ is in position } i]\). In a uniformly random permutation of 52 cards, each card is equally likely to be in any of the 52 positions. So:
$$ \E[X_i] = \frac{1}{52}. $$Therefore:
$$ \E[X] = 52 \cdot \frac{1}{52} = 1. $$Notice what just happened. The \(X_i\)’s are not independent — if card 1 is in position 1, that changes the conditional distribution of card 2’s position. But linearity of expectation doesn’t care about independence. We never needed to compute \(\Pr[X_i = 1 \text{ and } X_j = 1]\) or any joint probability. We only needed the marginal \(\Pr[X_i = 1]\) for each \(i\).
This is why linearity of expectation is the single most important technique in competitive probability. It lets you decompose a complex, globally-dependent random variable into trivial pieces.
To find the expected value of a count, write it as a sum of indicator variables (one per “thing” that might be counted), compute the marginal probability of each indicator, and add them up. Independence is irrelevant.
When to use it: any time you see “expected number of...” in a problem. Define an indicator for each potential occurrence and add up the probabilities. This is almost always the right move.
Other applications: expected number of inversions in a permutation, expected number of edges in a random graph, expected number of empty bins in a ball-into-bins problem, expected number of left-to-right maxima.
Going Deeper: Indicators at Interview Difficulty
The fixed-points puzzle is a warm-up. In a real quant interview, they will push further — harder setups, follow-up questions, and traps designed to test whether you truly understand the technique or have merely memorized one application. The four puzzles below escalate through the sub-skills that separate a good answer from an offer.
3a. Inversions in a Random Permutation
A deck of \(n\) cards, numbered 1 through \(n\), is shuffled into a uniformly random order. An inversion is a pair of cards \((i, j)\) with \(i < j\) such that card \(i\) appears after card \(j\) in the shuffled deck.
What is the expected number of inversions?
Before reading on: for \(n = 3\), there are \(3! = 6\) possible shuffles. Try listing all six and counting inversions in each. What is the average?
The wrong approach
The brute-force approach is to enumerate all \(n!\) permutations, count inversions in each, and average. Even for \(n = 10\), this is \(10! = 3{,}628{,}800\) permutations. For the general formula you would need to sum over all permutations using a generating function or a recurrence — doable, but far more work than necessary.
Define one indicator per pair, not per element. For each pair \((i, j)\) with \(i < j\), define \(X_{ij} = 1\) if \(i\) and \(j\) are inverted. The total number of inversions is \(X = \sum_{i < j} X_{ij}\). By linearity of expectation, you only need \(\Pr[X_{ij} = 1]\) for each pair.
For any pair \((i, j)\) with \(i < j\), consider only the relative order of cards \(i\) and \(j\) in the shuffled deck. In a uniformly random permutation, card \(i\) is equally likely to appear before or after card \(j\) — there are only two relative orderings and both are equally likely. So:
$$ \Pr[X_{ij} = 1] = \frac{1}{2}. $$There are \(\binom{n}{2}\) pairs. By linearity of expectation:
$$ \E[X] = \sum_{i < j} \E[X_{ij}] = \binom{n}{2} \cdot \frac{1}{2} = \frac{n(n-1)}{4}. $$| Permutation | Inversions | Count |
|---|---|---|
| (1, 2, 3) | none | 0 |
| (1, 3, 2) | (2,3) | 1 |
| (2, 1, 3) | (1,2) | 1 |
| (2, 3, 1) | (1,2), (1,3) | 2 |
| (3, 1, 2) | (1,3), (2,3) | 2 |
| (3, 2, 1) | (1,2), (1,3), (2,3) | 3 |
Average: \((0 + 1 + 1 + 2 + 2 + 3)/6 = 9/6 = 3/2\). Formula: \(3 \cdot 2 / 4 = 3/2\). \(\checkmark\)
The lesson: when counting pairs, edges, or collisions, the natural “atom” is a pair, not an individual element. You get \(\binom{n}{2}\) indicators instead of \(n\). The symmetry argument — “each pair is equally likely to be in either relative order” — is one of the most powerful one-liners in all of combinatorics.
Why interviewers love this: inversions are the natural measure of “how far from sorted” a permutation is. They arise in sorting algorithms (every comparison-based sort must resolve inversions), rank correlation (Kendall’s tau), and finance (measuring the disorder between two ranked portfolios).
3b. Left-to-Right Maxima (Records)
Shuffle \(n\) cards numbered 1 through \(n\). Read them left to right. A card is a record (or left-to-right maximum) if its value is strictly larger than every card before it. The first card is always a record.
What is the expected number of records?
Before reading on: for a shuffled deck of 52 cards, is the expected number of records closer to 1, 5, 10, or 26?
The wrong approach
You might try to track which card values appear in which positions. But the identity of the record depends on all previous cards — whether position 37 is a record depends on the maximum of positions 1 through 36. Tracking these joint dependencies across all positions seems to require analyzing the full permutation structure.
Define \(X_i = 1\) if the card at position \(i\) is a record. Ask: what is \(\Pr[X_i = 1]\)? The card at position \(i\) is a record if and only if it is the largest among the first \(i\) cards. Since the first \(i\) cards are a uniformly random subset in a uniformly random order, each of the \(i\) cards is equally likely to be the largest. So \(\Pr[X_i = 1] = 1/i\).
Let \(X_i = 1\{\text{position } i \text{ is a record}\}\). The card at position \(i\) is a record iff it is the maximum of the first \(i\) cards. In a uniformly random permutation, any of the first \(i\) cards is equally likely to be the maximum, so:
$$ \Pr[X_i = 1] = \frac{1}{i}. $$By linearity of expectation:
$$ \E[\text{records}] = \sum_{i=1}^{n} \Pr[X_i = 1] = \sum_{i=1}^{n} \frac{1}{i} = H_n. $$The harmonic number appears again — the same \(H_n\) from the coupon collector. For \(n = 52\): \(H_{52} \approx 4.56\), so a shuffled deck has about 4–5 records on average. For \(n = 10^6\): about 14.4 records. The number of records grows logarithmically, which means records are vanishingly rare relative to the deck size.
| Permutation | Records | Count |
|---|---|---|
| (1, 2, 3) | 1, 2, 3 | 3 |
| (1, 3, 2) | 1, 3 | 2 |
| (2, 1, 3) | 2, 3 | 2 |
| (2, 3, 1) | 2, 3 | 2 |
| (3, 1, 2) | 3 | 1 |
| (3, 2, 1) | 3 | 1 |
Average: \((3 + 2 + 2 + 2 + 1 + 1)/6 = 11/6\). Formula: \(H_3 = 1 + 1/2 + 1/3 = 11/6\). \(\checkmark\)
The lesson: not all indicators have the same probability. In the fixed-points problem, every \(\Pr[X_i = 1] = 1/n\) by symmetry, so the sum was \(n \cdot (1/n) = 1\). Here, the probabilities are \(1, 1/2, 1/3, \ldots, 1/n\), and the sum is the harmonic series. Recognizing which symmetry gives you each indicator’s probability is the core skill.
Why interviewers love this: records model real phenomena — record temperatures, record stock prices, record athletic performances. The fact that E[records] = \(H_n \approx \ln n\) explains why new records become increasingly rare over time: it’s not that performance is plateauing, it’s that the threshold keeps rising.
3c. Balls into Bins — Empty Bins
Throw \(m\) balls independently and uniformly at random into \(n\) bins. What is the expected number of empty bins?
Before reading on: if you throw 100 balls into 100 bins, roughly how many bins are empty? Is the answer closer to 0, 10, 37, or 50?
The wrong approach
The temptation is to track which specific balls land in which bins — a \(n^m\) sample space with complex dependencies. Even reasoning about a single bin requires inclusion-exclusion over which balls land in it. And then you’d need to combine the probabilities across all bins, which are not independent (if bin 1 is full, the other bins are slightly more likely to be empty).
Define \(X_k = 1\{\text{bin } k \text{ is empty}\}\). To compute \(\Pr[\text{bin } k \text{ is empty}]\), use complement counting within the indicator: bin \(k\) is empty iff all \(m\) balls missed it. Each ball independently misses bin \(k\) with probability \(1 - 1/n\). So the probability is a product.
For each bin \(k\), define \(X_k = 1\{\text{bin } k \text{ is empty}\}\). Each ball lands in bin \(k\) with probability \(1/n\), independently. So:
$$ \Pr[X_k = 1] = \left(1 - \frac{1}{n}\right)^m. $$By symmetry, this probability is the same for all \(n\) bins. By linearity of expectation:
$$ \E[\text{empty bins}] = n \left(1 - \frac{1}{n}\right)^m. $$The important special case: when \(m = n\) (as many balls as bins), we use the approximation \((1 - 1/n)^n \to 1/e\) as \(n \to \infty\):
$$ \E[\text{empty bins}] \approx \frac{n}{e} \approx 0.368n. $$With 100 balls into 100 bins: about 37 bins are empty. More than a third! This is deeply counterintuitive — even though there are enough balls to fill every bin, the random clumping leaves a substantial fraction empty.
In a real interview, the empty-bins question is just the opening. Here are the follow-ups, all solvable with the same technique:
Expected number of bins with exactly one ball (singletons):
Define \(Y_k = 1\{\text{bin } k \text{ has exactly 1 ball}\}\). Exactly one of the \(m\) balls lands in bin \(k\), and the other \(m - 1\) miss it:
$$ \Pr[Y_k = 1] = \binom{m}{1} \cdot \frac{1}{n} \cdot \left(1 - \frac{1}{n}\right)^{m-1} = \frac{m}{n}\left(1 - \frac{1}{n}\right)^{m-1}. $$So \(\E[\text{singletons}] = m(1 - 1/n)^{m-1}\). For \(m = n\): approximately \(n/e \approx 0.368n\).
Expected number of collision bins (\(\geq 2\) balls):
A collision bin is one that is neither empty nor a singleton:
$$ \E[\text{collision bins}] = n - \E[\text{empty}] - \E[\text{singletons}]. $$For \(m = n\): approximately \(n - n/e - n/e = n(1 - 2/e) \approx 0.264n\).
The lesson: when the indicator’s event is hard to compute directly (“is bin \(k\) non-empty?” requires tracking all \(m\) balls), flip to the complement (“is bin \(k\) empty?” = every ball missed it = a product of independent events). This is complement counting inside the indicator — combining two techniques from this post.
Why interviewers love this: balls-into-bins is the abstraction behind hash tables (bins = slots, balls = keys), the birthday problem (collision = shared birthday), load balancing (bins = servers, balls = requests), and coupon collecting. The empty-bin formula immediately gives the expected number of hash collisions, the expected load on the busiest server, and more. If you can solve the base case fluently, you can handle any follow-up.
3d. The Variance of Fixed Points
Return to the hat-checking problem: \(n\) people check their hats at a restaurant and receive a uniformly random permutation of hats on the way out. Let \(X\) be the number of people who get their own hat back.
We already know \(\E[X] = 1\). Now: what is \(\Var(X)\)?
Before reading on: is the variance large or small? Does it grow with \(n\)?
The wrong approach
The temptation is to compute \(\Var(X)\) by finding the full distribution of \(X\). This requires the number of permutations with exactly \(k\) fixed points for each \(k\) — a problem that requires inclusion-exclusion over all \(\binom{n}{k}\) subsets. The exact formula involves an alternating sum that converges to Poisson probabilities. It works, but it is a sledgehammer.
Use the identity \(\Var(X) = \E[X^2] - (\E[X])^2\). We already know \(\E[X] = 1\). So we just need \(\E[X^2]\).
Since \(X = \sum_i X_i\) where each \(X_i\) is an indicator (0 or 1), we have \(X_i^2 = X_i\), so:
$$ X^2 = \left(\sum_i X_i\right)^2 = \sum_i X_i^2 + \sum_{i \neq j} X_i X_j = \sum_i X_i + \sum_{i \neq j} X_i X_j. $$Therefore: \(\E[X^2] = \E[X] + \sum_{i \neq j} \E[X_i X_j]\). The whole problem reduces to computing one joint probability: \(\E[X_i X_j] = \Pr[\text{card } i \text{ in position } i \text{ AND card } j \text{ in position } j]\).
Step 1: Compute \(\E[X_i X_j]\) for \(i \neq j\).
Since \(X_i\) and \(X_j\) are indicators, \(X_i X_j = 1\) iff both card \(i\) is in position \(i\) and card \(j\) is in position \(j\). In a uniformly random permutation of \(n\) cards:
$$ \Pr[X_i X_j = 1] = \Pr[\text{card } i \text{ in pos } i] \cdot \Pr[\text{card } j \text{ in pos } j \mid \text{card } i \text{ in pos } i]. $$The first probability is \(1/n\). Given card \(i\) is fixed, card \(j\) must be in position \(j\) among the remaining \(n - 1\) cards in \(n - 1\) positions:
$$ \Pr[X_i X_j = 1] = \frac{1}{n} \cdot \frac{1}{n-1} = \frac{1}{n(n-1)}. $$Step 2: Sum over all pairs.
There are \(n(n-1)\) ordered pairs \((i, j)\) with \(i \neq j\):
$$ \sum_{i \neq j} \E[X_i X_j] = n(n-1) \cdot \frac{1}{n(n-1)} = 1. $$Step 3: Compute \(\E[X^2]\) and \(\Var(X)\).
$$ \E[X^2] = \E[X] + \sum_{i \neq j} \E[X_i X_j] = 1 + 1 = 2. $$ $$ \Var(X) = \E[X^2] - (\E[X])^2 = 2 - 1^2 = 1. $$Absorb this: the variance is exactly 1 whether you have 10 hats or 10 million hats. The mean is 1, the variance is 1, and the standard deviation is 1. The number of fixed points does not “spread out” as \(n\) grows — it stays tightly concentrated around its mean.
This is not a coincidence. The distribution of fixed points converges to Poisson(1) as \(n \to \infty\). A Poisson distribution with parameter \(\lambda\) has both mean and variance equal to \(\lambda\). The fact that mean = variance = 1 is the fingerprint of Poisson convergence, and you can prove it by showing that all factorial moments \(\E[X(X-1)\cdots(X-k+1)] = 1\) for every \(k\) — the same pair-counting trick, generalized.
The variance question is the inevitable follow-up to any expected-value problem using indicators. The formula is always the same:
$$ \Var(X) = \E[X] + \sum_{i \neq j} \E[X_i X_j] - (\E[X])^2. $$The hard part is computing the joint probability \(\E[X_i X_j] = \Pr[\text{both } i \text{ and } j \text{ fire}]\). Everything else is bookkeeping. If you can do this step cleanly, you will impress your interviewer.
The Indicator Variable Playbook
You have now seen indicator variables applied five different ways — to individual elements (fixed points), to pairs (inversions), to positions with non-uniform probabilities (records), with complement counting inside (balls into bins), and to compute variance (hat-checking). Here is the distilled playbook for deploying this technique under interview pressure.
The 3-Step Recipe
- Identify what you are counting. The answer to “expected number of ___” is a count. What are the things being counted? Fixed points, inversions, empty bins, records, collisions.
- Define one indicator per potential occurrence. Each “thing” that might be counted gets its own \(X_i \in \{0, 1\}\). The count is \(X = \sum X_i\). The key decision is what to index over — elements, pairs, triples, positions, bins.
- Compute each \(\Pr[X_i = 1]\) and sum. By linearity, \(\E[X] = \sum \Pr[X_i = 1]\). You never need joint probabilities for the expected value. You never need independence. Just marginals.
This is the hardest step, and it is where most candidates stumble. The rule of thumb:
- Counting individual things (fixed points, records, empty bins) → one indicator per element/position/bin. You get \(n\) indicators.
- Counting pairs (inversions, edges, collisions, shared birthdays) → one indicator per pair. You get \(\binom{n}{2}\) indicators.
- Counting triples (triangles in a graph, three-way collisions) → one indicator per triple. You get \(\binom{n}{3}\) indicators.
If you find yourself trying to track complex interactions between indicators, you probably chose the wrong atoms. Step back and ask: “what is the simplest object whose presence or absence I can indicate?”
When all indicators have the same probability by symmetry — as in fixed points (\(1/n\) each) or inversions (\(1/2\) each) — the expected value is just:
$$ \E[X] = (\text{number of indicators}) \times (\text{common probability}). $$This handles the majority of interview problems in one line. Only reach for non-uniform summation (records, weighted graphs) when the symmetry clearly breaks.
After you give the expected value, the interviewer will often ask: “What about the variance?” or “How concentrated is it around the mean?” Here is the recipe:
- Use \(\Var(X) = \E[X^2] - (\E[X])^2\).
- Since \(X_i^2 = X_i\) (indicators are 0 or 1): \(\E[X^2] = \E[X] + \sum_{i \neq j}\E[X_i X_j]\).
- Compute the joint probability \(\E[X_i X_j] = \Pr[\text{both fire}]\). This is the hard part. For independent indicators, \(\E[X_i X_j] = \E[X_i]\E[X_j]\), and the variance simplifies. For dependent indicators (permutations, sampling without replacement), you need to condition.
- Plug in: \(\Var(X) = \E[X] + \sum_{i \neq j}\E[X_i X_j] - (\E[X])^2\).
Common Interview Traps
Trap 1: “The indicators aren’t independent, so linearity doesn’t work.”
This is wrong. Linearity of expectation holds always. The proof is one line (we proved it above). Independence is irrelevant for expected values. If your interviewer says this, politely point to the proof: the sum distributes over the probability measure with no conditions.
Trap 2: Confusing “expected number” with “most likely number.”
For fixed points, \(\E[X] = 1\). But the most likely value (the mode) is 0 — about \(1/e \approx 36.8\%\) of permutations are derangements (zero fixed points). The expected value is pulled up by rare permutations with many fixed points. Don’t confuse mean and mode.
Trap 3: Forgetting to sanity-check.
Always verify your formula for \(n = 2\) or \(n = 3\) by exhaustive enumeration. This takes 30 seconds and catches algebraic errors that would otherwise sink you. If your formula says the expected number of inversions in a 3-card shuffle is anything other than 3/2, something is wrong.
The Master Pattern
| Problem | Indicate on | \(\Pr[X_i = 1]\) | \(\E[X]\) |
|---|---|---|---|
| Fixed points | Each card \(i\) | \(1/n\) | \(1\) |
| Inversions | Each pair \((i,j)\) | \(1/2\) | \(n(n\!-\!1)/4\) |
| Records | Each position \(i\) | \(1/i\) | \(H_n\) |
| Empty bins | Each bin \(k\) | \((1 - 1/n)^m\) | \(n(1 - 1/n)^m\) |
| Triangles in \(G(n,p)\) | Each triple | \(p^3\) | \(\binom{n}{3}p^3\) |
Once you see the pattern — choose the atom, compute the marginal, multiply — it becomes a reflex. The interviewer changes the setup; you change the indicator. The machinery is always the same.
4. The Coupon Collector
A cereal brand puts one of \(n\) different toy figures in each box, each equally likely. You want to collect the complete set of all \(n\) figures. What is the expected number of boxes you need to buy?
Before reading on: estimate the answer for \(n = 10\). Is it closer to 10, 20, 30, or 50?
The wrong approach
The naive guess is “about \(n\) boxes, since there are \(n\) types.” This drastically underestimates the true answer. The first few types come quickly, but the last few are agonizing — when you have 9 out of 10 types, each new box has only a 10% chance of being the missing type. You might buy 10 or 20 more boxes waiting for that last figure.
Break the process into phases. Phase \(k\) starts when you have \(k - 1\) distinct types and ends when you find the \(k\)-th new type. In phase \(k\), each box has probability \((n - k + 1)/n\) of being a new type. The waiting time in each phase is a geometric random variable, and the total is a sum of independent geometrics.
We need the expected value of a geometric random variable. Let’s derive it from scratch.
\(\Pr[X = k] = (1-p)^{k-1} p\) for \(k = 1, 2, 3, \ldots\). So:
$$ \E[X] = \sum_{k=1}^{\infty} k (1-p)^{k-1} p = p \sum_{k=1}^{\infty} k q^{k-1} \quad \text{where } q = 1-p. $$The inner sum is a standard calculus fact: \(\sum_{k=1}^\infty k q^{k-1} = \frac{d}{dq}\sum_{k=0}^\infty q^k = \frac{d}{dq}\frac{1}{1-q} = \frac{1}{(1-q)^2} = \frac{1}{p^2}\).
So \(\E[X] = p \cdot 1/p^2 = 1/p\). \(\square\)
Sanity check: if \(p = 1/6\) (rolling a die until you get a 6), \(\E[X] = 6\) rolls. If \(p = 1\) (always succeed), \(\E[X] = 1\). Both match intuition.
In phase \(k\) (collecting the \(k\)-th new type), you already have \(k-1\) types. Each box is a new type with probability:
$$ p_k = \frac{n - (k-1)}{n} = \frac{n - k + 1}{n}. $$The number of boxes in phase \(k\) is \(T_k \sim \operatorname{Geometric}(p_k)\), and the phases are independent (the outcome of each box is i.i.d.). The total number of boxes is \(T = T_1 + T_2 + \cdots + T_n\).
By linearity of expectation:
$$ \begin{aligned} \E[T] &= \sum_{k=1}^n \E[T_k] = \sum_{k=1}^n \frac{1}{p_k} = \sum_{k=1}^n \frac{n}{n - k + 1} \\ &= n \sum_{j=1}^n \frac{1}{j} = n \cdot H_n, \end{aligned} $$where \(H_n = 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}\) is the \(n\)-th harmonic number.
The harmonic number is one of the most important quantities in all of discrete mathematics. It appears in the coupon collector, in expected records of a random permutation (\(H_n\) again!), in the analysis of quicksort, in the 100 prisoners problem, and throughout number theory. Every serious quant candidate needs to know it cold: what it equals, why, and how to compute it. Let’s build the whole story from scratch.
Why \(H_n \approx \ln n\): the integral comparison
The key idea is to compare the sum \(\sum 1/k\) with the integral \(\int 1/x\, dx\). The function \(f(x) = 1/x\) is decreasing, so on each interval \([k, k+1]\):
$$ \frac{1}{k+1} \leq \int_k^{k+1} \frac{dx}{x} \leq \frac{1}{k}. $$Upper bound. Since \(1/x \leq 1/k\) for all \(x \in [k, k+1]\):
$$ \frac{1}{k} \geq \int_k^{k+1} \frac{dx}{x}. $$Sum from \(k = 1\) to \(n\):
$$ H_n = \sum_{k=1}^{n} \frac{1}{k} \geq \int_1^{n+1} \frac{dx}{x} = \ln(n+1). $$Lower bound. Since \(1/x \geq 1/(k+1)\) for all \(x \in [k, k+1]\):
$$ \frac{1}{k+1} \leq \int_k^{k+1} \frac{dx}{x}. $$Sum from \(k = 1\) to \(n-1\):
$$ H_n - 1 = \sum_{k=2}^{n} \frac{1}{k} = \sum_{k=1}^{n-1} \frac{1}{k+1} \leq \int_1^{n} \frac{dx}{x} = \ln n. $$Combining both:
$$ \ln(n+1) \leq H_n \leq 1 + \ln n. $$This proves \(H_n = \ln n + O(1)\). But we can be much more precise. \(\square\)
The Euler–Mascheroni constant: where the \(0.5772\ldots\) comes from
The bounds above show that \(H_n - \ln n\) is trapped between 0 and 1. But does this difference converge to a specific number? Define:
$$ \gamma_n = H_n - \ln n = \sum_{k=1}^{n} \frac{1}{k} - \ln n. $$We show \(\gamma_n\) is decreasing and bounded below, hence convergent by the monotone convergence theorem.
\(\gamma_n\) is decreasing.
$$ \begin{aligned} \gamma_n - \gamma_{n+1} &= \left(H_n - \ln n\right) - \left(H_{n+1} - \ln(n+1)\right) \\ &= \ln(n+1) - \ln n - \frac{1}{n+1} \\ &= \ln\!\left(1 + \frac{1}{n}\right) - \frac{1}{n+1}. \end{aligned} $$Since \(\ln(1 + 1/n) > 1/(n+1)\) for all \(n \geq 1\) (because \(\ln(1+x) > x/(1+x)\) for \(x > 0\), proved by noting the derivative of \(\ln(1+x) - x/(1+x)\) is \(x/(1+x)^2 > 0\)), we get \(\gamma_n > \gamma_{n+1}\). So \(\gamma_n\) is strictly decreasing.
\(\gamma_n\) is bounded below. We showed \(H_n \geq \ln(n+1)\), so:
$$ \gamma_n = H_n - \ln n \geq \ln(n+1) - \ln n = \ln\!\left(1 + \frac{1}{n}\right) > 0. $$A decreasing sequence bounded below converges. We call its limit:
$$ \gamma = \lim_{n \to \infty} \left(\sum_{k=1}^{n} \frac{1}{k} - \ln n\right) = 0.5772156649\ldots $$This is the Euler–Mascheroni constant. \(\square\)
So we have the precise asymptotic: \(H_n = \ln n + \gamma + O(1/n)\). For practical computation:
| \(n\) | \(H_n\) (exact) | \(\ln n + \gamma\) | Error |
|---|---|---|---|
| 10 | 2.92897 | 2.87983 | 0.049 |
| 100 | 5.18738 | 5.18239 | 0.005 |
| 1,000 | 7.48547 | 7.48497 | 0.0005 |
| 10,000 | 9.78761 | 9.78756 | 0.00005 |
The error shrinks as \(1/(2n)\), consistent with the refined expansion \(H_n = \ln n + \gamma + \frac{1}{2n} - \frac{1}{12n^2} + \cdots\).
Three techniques for computing harmonic-type sums
The integral comparison that proved \(H_n \approx \ln n\) is the most important technique, but it is far from the only one. Here are three methods every quant candidate should have ready.
Technique 1: Integral Comparison (Bounding)
For any decreasing function \(f\):
$$ \int_1^{n+1} f(x)\,dx \leq \sum_{k=1}^{n} f(k) \leq f(1) + \int_1^{n} f(x)\,dx. $$When to use: any sum of the form \(\sum f(k)\) where \(f\) is monotone and has a closed-form antiderivative. Works for \(\sum 1/k\), \(\sum 1/k^2\), \(\sum 1/\sqrt{k}\), \(\sum \ln k\), etc.
Example: \(\sum_{k=1}^{n} 1/k^2 \leq 1 + \int_1^n x^{-2}\,dx = 1 + (1 - 1/n) = 2 - 1/n < 2\). This proves the sum converges (the exact limit is \(\pi^2/6\)).
Technique 2: Telescoping via Partial Fractions
Some harmonic-type sums collapse via partial fractions. For example:
$$ \sum_{k=1}^{n} \frac{1}{k(k+1)} = \sum_{k=1}^{n} \left(\frac{1}{k} - \frac{1}{k+1}\right) = 1 - \frac{1}{n+1} = \frac{n}{n+1}. $$When to use: when the summand factors as a product of consecutive terms, or can be decomposed into a telescoping difference. Common in interview problems involving \(\sum 1/(k(k+1))\), \(\sum 1/(k(k+1)(k+2))\), or more generally any sum arising from partial fraction decomposition.
Key identity: \(\frac{1}{k(k+1)\cdots(k+r)} = \frac{1}{r}\left(\frac{1}{k(k+1)\cdots(k+r-1)} - \frac{1}{(k+1)(k+2)\cdots(k+r)}\right)\).
Technique 3: The Integral Representation
The harmonic number has a beautiful integral form:
$$ H_n = \int_0^1 \frac{1 - x^n}{1 - x}\,dx. $$Proof: expand \(\frac{1 - x^n}{1-x} = 1 + x + x^2 + \cdots + x^{n-1}\) (geometric series) and integrate term by term:
$$ \int_0^1 (1 + x + \cdots + x^{n-1})\,dx = 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n} = H_n. \quad \square $$When to use: when you need to evaluate \(H_n\) as part of a larger integral or generating-function calculation. Also useful for proving identities like \(\gamma = \int_0^1 \left(\frac{1}{1-x} + \frac{1}{\ln x}\right) dx\), or for connecting harmonic numbers to the digamma function \(\psi(n+1) = -\gamma + H_n\).
Memorize these values:
- \(H_1 = 1\), \(H_2 = 3/2\), \(H_3 = 11/6\), \(H_4 = 25/12\), \(H_{10} \approx 2.93\), \(H_{100} \approx 5.19\).
- \(\gamma \approx 0.5772\). It is unknown whether \(\gamma\) is rational or irrational.
- \(H_n \approx \ln n + 0.5772\). For quick mental math: \(\ln 10 \approx 2.303\), so \(H_{100} \approx 2 \times 2.303 + 0.577 \approx 5.18\).
If you can produce \(H_{100} \approx 5.19\) from memory in an interview, the interviewer knows you’ve done the work.
Now we can state the coupon collector’s answer precisely. Since \(H_n = \ln n + \gamma + O(1/n)\):
| \(n\) | \(H_n\) | \(\E[\text{boxes}] = n \cdot H_n\) |
|---|---|---|
| 6 (die faces) | 2.45 | 14.7 |
| 10 | 2.93 | 29.3 |
| 50 (US states) | 4.50 | 225 |
| 100 | 5.19 | 519 |
For 50 state quarters: you need to buy about 225 random quarters to collect all 50. The last few states are brutal — phase 50 alone costs an expected \(50/1 = 50\) quarters.
When a process has a sequence of “waiting for the next event” phases, each with a fixed success probability, model each phase as a geometric random variable. The total time is a sum of geometrics, and linearity handles the rest.
When to use it: any “how long until...” problem where the process has natural phases with changing success probabilities. Also works for expected time to fill bins, complete graphs, or cover sets.
Going Deeper: The Coupon Collector at Interview Difficulty
The basic coupon collector is a warm-up. In a real quant interview, they’ll push you past the expected value: what’s the variance? What if you flip the question around? What if the coupons aren’t uniform? The four puzzles below escalate through the sub-skills that separate a clean answer from a stumble.
4a. The Collector’s Variance
There are \(n\) coupon types. Each box contains a uniformly random coupon. Let \(T\) be the number of boxes you buy to collect all \(n\) types. We know \(\E[T] = nH_n\). What is \(\Var(T)\)?
Before reading on: is the coupon collector tightly concentrated around its mean, or is there a lot of variability? Try guessing whether \(\sigma(T)\) is much smaller than, comparable to, or much larger than \(\E[T]\).
The wrong approach
You might try to compute \(\E[T^2]\) directly by summing over all possible collection sequences. This is a combinatorial nightmare — the phases are interleaved and the bookkeeping explodes. The right move is to exploit the phase decomposition we already have.
The geometric phases \(T_1, T_2, \ldots, T_n\) are independent. Each phase starts fresh — the number of boxes you buy in phase \(k\) depends only on the success probability \(p_k\), not on what happened in earlier phases. Since \(T = T_1 + \cdots + T_n\) and the phases are independent, \(\Var(T) = \sum_{k=1}^{n} \Var(T_k)\).
Step 1: Variance of a Geometric random variable. Let \(X \sim \text{Geometric}(p)\) (number of trials until the first success). We need \(\E[X^2]\). Recall \(\E[X] = 1/p\). We derive \(\E[X^2]\) from scratch using the same conditioning trick that gives \(\E[X]\).
Let \(q = 1 - p\). On the first trial, either we succeed (probability \(p\), and \(X = 1\)) or we fail (probability \(q\), and we start over with \(X = 1 + X'\) where \(X' \sim \text{Geometric}(p)\) is an independent copy). Conditioning:
$$ \E[X^2] = p \cdot 1^2 + q \cdot \E[(1 + X')^2] = p + q\,\E[1 + 2X' + X'^2]. $$Since \(X'\) has the same distribution as \(X\):
$$ \E[X^2] = p + q\bigl(1 + 2\E[X] + \E[X^2]\bigr) = p + q + 2q\E[X] + q\,\E[X^2]. $$Using \(p + q = 1\) and \(\E[X] = 1/p\):
$$ \E[X^2] = 1 + \frac{2q}{p} + q\,\E[X^2]. $$Solving for \(\E[X^2]\):
$$ \begin{aligned} \E[X^2] - q\,\E[X^2] &= 1 + \frac{2q}{p} \\[4pt] p\,\E[X^2] &= 1 + \frac{2q}{p} = \frac{p + 2q}{p} = \frac{2 - p}{p} \\[4pt] \E[X^2] &= \frac{2 - p}{p^2}. \end{aligned} $$So:
$$ \Var(X) = \E[X^2] - (\E[X])^2 = \frac{2 - p}{p^2} - \frac{1}{p^2} = \frac{1 - p}{p^2}. $$Step 2: Apply to each phase. In phase \(k\), you have already collected \(k - 1\) types, so the success probability is \(p_k = (n - k + 1)/n\). Substituting \(j = n - k + 1\) (so \(j\) runs from \(n\) down to \(1\)):
$$ \Var(T_k) = \frac{1 - p_k}{p_k^2} = \frac{1 - j/n}{(j/n)^2} = \frac{n^2 - nj}{j^2} = \frac{n^2}{j^2} - \frac{n}{j}. $$Step 3: Sum the variances. Since the phases are independent:
$$ \begin{aligned} \Var(T) &= \sum_{j=1}^{n} \left(\frac{n^2}{j^2} - \frac{n}{j}\right) \\ &= n^2 \sum_{j=1}^{n} \frac{1}{j^2} \;-\; n \sum_{j=1}^{n} \frac{1}{j} \\ &= n^2 \sum_{j=1}^{n} \frac{1}{j^2} \;-\; n \cdot H_n. \end{aligned} $$Step 4: Asymptotics. As \(n \to \infty\), \(\sum_{j=1}^{n} 1/j^2 \to \pi^2/6\) (the Basel problem). So:
$$ \Var(T) \;\approx\; \frac{\pi^2}{6}\, n^2 \quad \text{and} \quad \sigma(T) \;\approx\; \frac{\pi}{\sqrt{6}}\, n \;\approx\; 1.28\, n. $$The Basel Problem: why \(\pi\) appears in \(\sum 1/k^2\)
We just used the identity \(\sum_{k=1}^{\infty} 1/k^2 = \pi^2/6\) to close the variance calculation. But where does \(\pi\) — the ratio of a circle’s circumference to its diameter — come from in a sum of reciprocal squares? This is the Basel problem, posed in 1644 and unsolved for 90 years until Euler cracked it in 1735. It is one of the most beautiful results in all of mathematics, and understanding why it is true is worth far more than memorizing the answer.
The circle constant \(\pi\) enters because \(\sum 1/k^2\) is secretly connected to the sine function. The function \(\sin(x)\) has zeros at every integer multiple of \(\pi\): at \(0, \pm\pi, \pm 2\pi, \pm 3\pi, \ldots\) These zeros, spaced \(\pi\) apart, are what plant \(\pi\) inside the sum. Euler’s proof makes this connection explicit by factoring \(\sin(x)/x\) as an infinite product from its roots — exactly as you would factor a polynomial — and then reading off the answer from a single coefficient.
Step 1: The Taylor series of \(\sin(x)/x\). From the well-known expansion \(\sin(x) = x - x^3/3! + x^5/5! - \cdots\), divide both sides by \(x\):
$$ \frac{\sin x}{x} = 1 - \frac{x^2}{6} + \frac{x^4}{120} - \cdots $$The coefficient of \(x^2\) on this side is \(-1/6\). Remember this.
Step 2: The zeros of \(\sin(x)/x\). The function \(\sin(x)\) vanishes at \(x = 0, \pm\pi, \pm 2\pi, \pm 3\pi, \ldots\) After dividing out the root at \(0\), the function \(\sin(x)/x\) has zeros precisely at \(x = \pm n\pi\) for \(n = 1, 2, 3, \ldots\)
Step 3: Factor from the roots. A polynomial with roots \(r_1, r_2, \ldots, r_N\) and value \(1\) at \(x = 0\) factors as \(\prod_{i=1}^{N}(1 - x/r_i)\). Euler’s bold move was to extend this to the infinite product for \(\sin(x)/x\), pairing each root \(+n\pi\) with \(-n\pi\) into a single quadratic factor:
$$ \frac{\sin x}{x} = \prod_{n=1}^{\infty} \left(1 - \frac{x^2}{n^2 \pi^2}\right). $$(This factorization is justified rigorously by the Weierstrass factorization theorem — see the remark below.)
Step 4: Compare \(x^2\) coefficients. Expand the infinite product. The \(x^2\) term arises by selecting the \(-x^2/(n^2\pi^2)\) factor from exactly one term and \(1\) from all others:
$$ \text{coefficient of } x^2 \text{ in the product} = -\sum_{n=1}^{\infty} \frac{1}{n^2 \pi^2} = -\frac{1}{\pi^2}\sum_{n=1}^{\infty} \frac{1}{n^2}. $$From Step 1, this coefficient equals \(-1/6\). Equating:
$$ -\frac{1}{\pi^2}\sum_{n=1}^{\infty} \frac{1}{n^2} = -\frac{1}{6} \quad\implies\quad \sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}. \quad\square $$Euler’s argument assumes that \(\sin(x)/x\) can be factored from its roots like a polynomial. This is true, and is guaranteed by the Weierstrass factorization theorem: an entire function of finite order with known zeros is determined (up to an exponential factor) by those zeros. For \(\sin(x)/x\), the exponential factor is \(1\) (proved by bounding the growth rate), so the product representation is exact.
For a completely different approach that avoids infinite products, one can evaluate the double integral \(\int_0^1\!\!\int_0^1 \frac{1}{1-xy}\,dx\,dy\) two ways: expand as a geometric series to get \(\sum 1/k^2\), and apply a 45° rotation to reduce it to an arctangent integral yielding \(\pi^2/6\).
| \(n\) | \(\sum_{k=1}^{n} 1/k^2\) | \(\pi^2/6\) | Error |
|---|---|---|---|
| 1 | 1.000000 | 1.644934 | 0.6449 |
| 10 | 1.549768 | 1.644934 | 0.0952 |
| 100 | 1.634984 | 1.644934 | 0.0100 |
| 1{,}000 | 1.643935 | 1.644934 | 0.0010 |
| 10{,}000 | 1.644834 | 1.644934 | 0.0001 |
The error decreases as \(1/n\), consistent with integral comparison: the tail \(\sum_{k=n+1}^{\infty} 1/k^2 \approx \int_n^{\infty} x^{-2}\,dx = 1/n\).
The standard deviation \(\sigma(T) \approx 1.28n\) is comparable to the mean \(\E[T] \approx n\ln n\). For \(n = 50\): \(\E[T] \approx 225\) but \(\sigma(T) \approx 64\), so a one-standard-deviation swing is roughly 30% of the mean. The coupon collector is not tightly concentrated — there is real variability, driven by the brutal final phases where you’re searching for one specific coupon among \(n\).
Sub-skill learned: when geometric phases are independent, variances add. Know \(\Var(\text{Geometric}(p)) = (1-p)/p^2\) cold.
4b. Distinct Values After \(m\) Draws
There are \(n\) coupon types. You draw \(m\) coupons uniformly at random (with replacement). What is the expected number of distinct types you see?
Before reading on: this looks like the coupon collector, but the question is inverted. Instead of “how many draws until all \(n\)?” it asks “how many distinct after \(m\)?” Does geometric decomposition work here?
The wrong approach
Trying to use geometric phases fails immediately — you’re not waiting for the “next new type” in sequence, you’re drawing a fixed number of times and counting what you got. There are no natural phases. If you try to track which types appear in which order, you end up in an inclusion-exclusion mess with \(2^n\) subsets.
This is an indicator problem, not a geometric problem. For each coupon type \(k\), define \(D_k = 1\) if type \(k\) appears at least once in the \(m\) draws. The number of distinct types is \(D = D_1 + D_2 + \cdots + D_n\). By linearity of expectation, you just need \(\Pr[D_k = 1]\) — and this is easy to compute via the complement.
Step 1: Probability a specific type appears. Type \(k\) is missing from all \(m\) draws when every draw lands on one of the other \(n - 1\) types:
$$ \Pr[D_k = 0] = \left(1 - \frac{1}{n}\right)^m. $$So \(\Pr[D_k = 1] = 1 - (1 - 1/n)^m\). By symmetry, this is the same for every type.
Step 2: Linearity of expectation.
$$ \E[D] = \sum_{k=1}^{n} \Pr[D_k = 1] = n\left(1 - \left(1 - \frac{1}{n}\right)^m\right). $$| \(m\) | \(\E[\text{distinct}]\) for \(n = 50\) | Interpretation |
|---|---|---|
| \(1\) | \(1\) | First draw always gives a new type |
| \(50\) | \(50(1 - (49/50)^{50}) \approx 31.8\) | After \(n\) draws, about 63% collected |
| \(225 \approx nH_n\) | \(\approx 49.0\) | At the coupon collector mean, almost done |
| \(\to \infty\) | \(\to 50\) | Eventually all types appear |
Sub-skill learned: recognizing when a coupon-collector-looking problem is actually an indicator problem. The direction switch — from “time until target” to “count after fixed time” — changes the technique completely.
4c. The First Collision
There are \(n\) coupon types. You draw coupons one at a time, uniformly at random. What is the expected number of draws until you see the first duplicate coupon?
Before reading on: this might look like a coupon collector variant, but you’re not waiting for a success (new type) — you’re waiting for a failure (repeated type). Does geometric decomposition still apply?
Why geometric decomposition fails
In the coupon collector, each phase waits for a success: “draw until you get a new type.” Here you’re waiting for a failure: “draw until you get an old type.” You could try to set up a geometric with \(p_k = (k-1)/n\) on draw \(k\), but the phases aren’t independent — which types have appeared depends on the entire history. More fundamentally, you stop at the first failure, so there’s only one “phase,” and it doesn’t decompose.
This is the birthday problem in coupon clothing. Let \(C\) be the draw on which the first collision occurs. The event \(\{C > m\}\) (no collision in the first \(m\) draws) requires all \(m\) draws to be distinct — exactly the birthday problem condition.
Step 1: Probability of no collision in \(m\) draws. Draw 1 is always new. Draw 2 avoids 1 type with probability \((n-1)/n\). Draw 3 avoids 2 types with probability \((n-2)/n\). Continuing:
$$ \Pr[C > m] = \prod_{k=0}^{m-1} \frac{n - k}{n} = \prod_{k=0}^{m-1}\left(1 - \frac{k}{n}\right). $$Step 2: Approximate using \(\ln(1-x) \approx -x\).
$$ \begin{aligned} \ln \Pr[C > m] &= \sum_{k=0}^{m-1} \ln\!\left(1 - \frac{k}{n}\right) \\ &\approx -\sum_{k=0}^{m-1} \frac{k}{n} = -\frac{m(m-1)}{2n}. \end{aligned} $$So \(\Pr[C > m] \approx e^{-m^2/(2n)}\), a Gaussian-style decay.
Step 3: Expected value via survival function.
$$ \begin{aligned} \E[C] &= \sum_{m=0}^{\infty} \Pr[C > m] \approx \int_0^{\infty} e^{-m^2/(2n)}\, dm \\ &= \sqrt{2n} \int_0^{\infty} e^{-u^2}\, du = \sqrt{2n} \cdot \frac{\sqrt{\pi}}{2} = \sqrt{\frac{\pi n}{2}}. \end{aligned} $$| \(n\) | \(\sqrt{\pi n/2}\) | Famous version |
|---|---|---|
| 365 | \(\approx 24.0\) | Birthday problem: ~23 people for 50% match |
| 50 | \(\approx 8.9\) | After ~9 random state quarters, expect a repeat |
| 1{,}000{,}000 | \(\approx 1{,}253\) | Hash collisions in a million-bucket table |
Contrast with the coupon collector: collecting all \(n = 365\) “birthday coupons” takes \(\E[T] \approx 365 \ln 365 \approx 2{,}153\) draws. The first collision comes after \(\approx 24\) — a factor of 90× sooner. The \(\sqrt{n}\) vs. \(n \ln n\) gap is enormous.
Sub-skill learned: recognizing when geometric decomposition doesn’t apply, and identifying the birthday problem hiding inside a coupon-collector setup.
4d. The Loaded Coupon Collector
Two coupon types: type A appears with probability \(p\), type B with probability \(1 - p\). You draw until you’ve seen both. What is the expected number of draws?
Before reading on: for \(p = 1/2\), the answer should match \(2 \cdot H_2 = 3\). What happens when \(p = 0.01\)? Try guessing before computing.
Why the standard decomposition breaks
In the standard coupon collector, phase \(k\) has success probability \((n - k + 1)/n\), and you decompose \(T\) into a sum of independent geometrics. This works because every coupon type is equally likely. With non-uniform probabilities, you can’t cleanly separate into phases — the “easy” coupon might keep appearing while you wait for the rare one, but you don’t know which one you’ll see first.
Use inclusion-exclusion on the maximum of geometric waiting times. Let \(T_A\) = first draw of type A (Geometric\((p)\)), \(T_B\) = first draw of type B (Geometric\((1-p)\)). You’re done when both have appeared, so \(T = \max(T_A, T_B)\). Use the identity:
$$ \E[\max(T_A, T_B)] = \E[T_A] + \E[T_B] - \E[\min(T_A, T_B)]. $$Step 1: The min-max identity. For any two random variables: \(\max(X, Y) = X + Y - \min(X, Y)\). Taking expectations:
$$ \E[\max(T_A, T_B)] = \E[T_A] + \E[T_B] - \E[\min(T_A, T_B)]. $$Step 2: Each term. \(T_A \sim \text{Geometric}(p)\), so \(\E[T_A] = 1/p\). Similarly \(\E[T_B] = 1/(1-p)\). The minimum \(\min(T_A, T_B)\) is the time until the first coupon of either type, which happens on every draw with probability \(p + (1-p) = 1\). So \(\min(T_A, T_B) = 1\) always (the first draw is always one of the two types).
Step 3: Combine.
$$ \E[T] = \frac{1}{p} + \frac{1}{1-p} - 1. $$| \(p\) | \(1/p + 1/(1-p) - 1\) | Interpretation |
|---|---|---|
| \(1/2\) | \(2 + 2 - 1 = 3\) | Matches \(2H_2 = 3\) — the uniform case |
| \(0.01\) | \(100 + 1.01 - 1 = 100.01\) | Dominated by the rare coupon |
| \(0.99\) | \(1.01 + 100 - 1 = 100.01\) | Symmetric — same answer |
| \(0.1\) | \(10 + 1.11 - 1 = 10.11\) | The 10:1 rarity ratio shows |
The general formula for \(n\) types with probabilities \(p_1, \ldots, p_n\) uses full inclusion-exclusion:
$$ \E[T] = \sum_{\emptyset \neq S \subseteq [n]} (-1)^{|S|+1} \;\frac{1}{\displaystyle\sum_{i \in S} p_i}. $$For the uniform case \(p_i = 1/n\), the sum over \(S\) with \(|S| = k\) contributes \(\binom{n}{k}(-1)^{k+1} \cdot n/k\). After simplification, this recovers \(nH_n\) — a satisfying consistency check.
Sub-skill learned: when non-uniform probabilities break the phase decomposition, use inclusion-exclusion via the min-max identity. The formula is beautiful but structurally different from the harmonic sum.
The Geometric Decomposition Playbook
Five patterns that cover every coupon-collector-family problem you’ll face.
(1) Identify the phases: what event marks the transition from “have collected \(k\)” to “have collected \(k+1\)”?
(2) Compute the success probability in each phase: \(p_k = (\text{number of “good” outcomes}) / (\text{total outcomes})\).
(3) Sum \(\E[\text{Geometric}(p_k)] = 1/p_k\) over all phases. Linearity handles the rest.
| Works | Fails |
|---|---|
| Waiting for \(n\) distinct successes in sequence | Waiting for the first failure (birthday/collision) |
| Uniform probabilities across types | Non-uniform probabilities (need inclusion-exclusion) |
| Independent trials | Dependent trials (e.g., sampling without replacement) |
| “How long until all?” questions | “How many distinct after \(m\)?” (use indicators) |
Interviewers love variance follow-ups. Know these facts cold:
Formula: \(\Var(\text{Geometric}(p)) = (1-p)/p^2\).
Independence: the geometric phases are independent, so \(\Var(T) = \sum \Var(T_k)\).
Punchline: \(\Var(T) \approx \pi^2 n^2/6\), giving \(\sigma \approx 1.28n\). The coupon collector has high variability.
“How many distinct after \(m\) draws?” is not a geometric problem — it’s an indicator problem. The direction switch changes the technique:
Forward (geometric): fix the target count, find the expected time.
Inverse (indicators): fix the time, find the expected count.
When you see a coupon-collector-looking problem, check which direction the question goes before reaching for the geometric decomposition.
| Problem | Technique | Answer |
|---|---|---|
| Collect all \(n\) (uniform) | Geometric decomposition | \(\E[T] = nH_n \approx n\ln n\) |
| Variance of collection time | Sum of geometric variances | \(\Var(T) \approx \pi^2 n^2 / 6\) |
| Distinct types after \(m\) draws | Indicator variables | \(n(1 - (1-1/n)^m)\) |
| First collision | Birthday/survival function | \(\approx \sqrt{\pi n/2}\) |
| Non-uniform collector | Min-max + inclusion-exclusion | \(\sum_{\emptyset \neq S} (-1)^{|S|+1}/\!\sum_{i \in S} p_i\) |
5. Gambler’s Ruin
A gambler starts with \(\$k\) and repeatedly bets \(\$1\) on a fair coin flip: heads, they win \(\$1\); tails, they lose \(\$1\). They stop when they either reach \(\$n\) (they win) or \(\$0\) (they are ruined). What is the probability they win?
Before reading on: if you start with \(\$50\) and want to reach \(\$100\), what is the probability? The coin is fair.
The wrong approach
The temptation is to simulate or enumerate paths. But a random walk between two absorbing barriers has infinitely many possible paths of every length — the gambler could wander back and forth for thousands of steps before eventually hitting a barrier. Direct enumeration is hopeless.
First-step analysis: condition on the very first move. After one flip, the gambler is at \(\$k+1\) or \(\$k-1\), each with probability \(1/2\). Whatever happens after that is a smaller version of the same problem. This gives a recurrence relation that we can solve.
Let \(p_k = \Pr[\text{reach } \$n \text{ starting from } \$k]\). By conditioning on the first flip:
$$ p_k = \frac{1}{2}\, p_{k+1} + \frac{1}{2}\, p_{k-1}, \qquad k = 1, 2, \ldots, n-1. $$Boundary conditions: \(p_0 = 0\) (ruin) and \(p_n = 1\) (win).
Solving the recurrence. Rearrange: \(p_{k+1} - p_k = p_k - p_{k-1}\). This says the successive differences are constant. Let \(d = p_1 - p_0 = p_1\). Then:
$$ p_k - p_{k-1} = d \quad \text{for all } k, $$so \(p_k = k \cdot d\) (telescoping from \(p_0 = 0\)). From the boundary condition \(p_n = 1\):
$$ n \cdot d = 1 \implies d = \frac{1}{n} \implies p_k = \frac{k}{n}. $$This is beautifully intuitive. With a fair coin, your probability of winning is proportional to your starting fraction of the total. Start with half the money (\(k = n/2\)) and you have a 50% chance — exactly what fairness predicts.
Extension: the biased coin
What if the coin lands heads with probability \(p \neq 1/2\)? The recurrence becomes:
$$ p_k = p \cdot p_{k+1} + (1-p) \cdot p_{k-1}. $$We can solve this by the same “telescoping differences” approach, or by a more powerful general method. Let’s see both.
Method 1: Telescoping differences. Rearrange:
$$ p \cdot (p_{k+1} - p_k) = (1-p) \cdot (p_k - p_{k-1}), $$so the ratio of successive differences is constant: \(r = (1-p)/p\). The differences form a geometric series: \(p_k - p_{k-1} = d \cdot r^{k-1}\). Summing from \(p_0 = 0\):
$$ p_k = d \cdot \frac{r^k - 1}{r - 1}, \qquad p_n = 1 \implies p_k = \frac{r^k - 1}{r^n - 1}, \quad r = \frac{1-p}{p}. $$Method 2: The characteristic equation. This is the general method for any linear recurrence with constant coefficients. Guess \(p_k = x^k\) and substitute into the recurrence \(p \cdot p_{k+1} - p_k + (1-p) \cdot p_{k-1} = 0\):
$$ x^{k-1}\!\left(p \cdot x^2 - x + (1-p)\right) = 0 \quad\implies\quad (x - 1)\!\left(px - (1-p)\right) = 0. $$Roots: \(x_1 = 1\) and \(x_2 = (1-p)/p = r\). Two distinct roots, so the general solution is \(p_k = A \cdot 1^k + B \cdot r^k = A + Br^k\). Apply boundaries \(p_0 = 0\) and \(p_n = 1\) to find \(A\) and \(B\), recovering the same answer \(p_k = (r^k - 1)/(r^n - 1)\).
This method generalizes to every recurrence in this section — including the non-homogeneous and multi-state versions below.
At a casino with \(p = 0.49\) (house edge of 2%), starting with \(\$50\) and trying to reach \(\$100\):
$$ r = \frac{0.51}{0.49} \approx 1.0408, \qquad p_{50} = \frac{r^{50} - 1}{r^{100} - 1} \approx \frac{6.58}{49.9} \approx 0.132. $$Only a 13.2% chance of doubling your money, down from 50% with a fair coin. A 2% edge might sound tiny, but over the long run of a random walk, it compounds into near-certain ruin.
Condition on the first random event to obtain a recurrence relation. If the state after one step is a smaller instance of the same problem, you get a solvable recurrence with boundary conditions.
When to use it: any problem with a Markov property — where the future depends only on the current state, not the history. Random walks, dice games, card games, queueing problems, Markov chain absorption times.
The method: (1) define the unknown as a function of the state, (2) condition on the first transition, (3) solve the resulting recurrence or system of equations.
Going Deeper: Recurrences at Interview Difficulty
The basic gambler’s ruin is a warm-up. In a real quant interview, they will push past the probability question: what is the expected duration of the game? What if the state space is more complex — multiple interacting states instead of a simple number line? What if the barrier is at infinity? The four puzzles below escalate through the sub-skills of recurrence relations that separate a clean answer from a stumble. Each one teaches a different type of recurrence — and together they form a complete toolkit for first-step analysis at interview difficulty.
5a. Expected Duration of the Game
A gambler starts with \(\$k\) and repeatedly bets \(\$1\) on a fair coin flip: heads \(+\$1\), tails \(-\$1\). They stop when they reach \(\$n\) or \(\$0\). What is the expected number of flips until the game ends?
Before reading on: for \(k = 50\) and \(n = 100\), is the expected duration closer to 50, 100, 500, or 2,500?
The wrong approach
Computing the probability that the walk takes exactly \(t\) steps requires enumerating lattice paths between two absorbing barriers of every possible length. The resulting sums involve binomial coefficients and do not collapse into a closed form. Direct summation is a dead end.
Same first-step analysis, new recurrence. Define \(E_k\) = expected number of flips starting from \(\$k\). After one flip, the gambler moves to \(k+1\) or \(k-1\) — but each flip costs one unit of time. The recurrence is:
$$ E_k = 1 + \frac{1}{2}\,E_{k+1} + \frac{1}{2}\,E_{k-1}. $$The \(+1\) is the critical difference from the probability recurrence. It makes this equation non-homogeneous, and the solution method changes entirely.
Step 1: Recurrence and boundaries.
$$ E_k = 1 + \frac{1}{2}\,E_{k+1} + \frac{1}{2}\,E_{k-1}, \qquad k = 1, \ldots, n-1. $$Boundaries: \(E_0 = E_n = 0\) (the game ends immediately at either barrier).
Step 2: Rearrange. Multiply through by 2 and collect terms:
$$ E_{k+1} - 2E_k + E_{k-1} = -2. $$This is a second-order linear recurrence with constant coefficients and a constant forcing term.
Step 3: Homogeneous solution. First, ignore the forcing term and solve the homogeneous version \(E_{k+1} - 2E_k + E_{k-1} = 0\). How do we solve a recurrence like this? The method is exactly analogous to solving differential equations like \(y'' - 2y' + y = 0\) — we guess an exponential.
The characteristic equation method. Guess \(E_k = x^k\) for some constant \(x\). Substituting into \(E_{k+1} - 2E_k + E_{k-1} = 0\):
$$ x^{k+1} - 2x^k + x^{k-1} = 0. $$Factor out \(x^{k-1}\) (which is never zero):
$$ x^{k-1}(x^2 - 2x + 1) = 0 \quad\implies\quad x^2 - 2x + 1 = 0. $$This quadratic is the characteristic equation. Its roots tell us which exponentials \(x^k\) solve the recurrence. There are three cases:
- Two distinct roots \(x_1 \neq x_2\): general solution is \(E_k = A \cdot x_1^k + B \cdot x_2^k\).
- One repeated root \(x_1 = x_2 = x_0\): the two independent solutions are \(x_0^k\) and \(k \cdot x_0^k\), so the general solution is \(E_k = (A + Bk) \cdot x_0^k\).
(This is the discrete analogue of \(e^{rx}\) and \(xe^{rx}\) for repeated roots in ODEs.)
Here: \(x^2 - 2x + 1 = (x - 1)^2 = 0\). Double root at \(x = 1\). Since \(1^k = 1\) for all \(k\), the two independent solutions are \(1\) and \(k \cdot 1^k = k\). So the homogeneous solution is:
$$ E_k^{(h)} = A + Bk. $$(Compare with the biased coin above: that recurrence has characteristic roots \(1\) and \(r = q/p\), two distinct roots, giving general solution \(A \cdot 1^k + B \cdot r^k = A + Br^k\).)
Step 4: Particular solution. The full equation is \(E_{k+1} - 2E_k + E_{k-1} = -2\). The right-hand side is a constant, so we want a particular solution that produces a constant when plugged into the left-hand side. Normally we’d try \(E_k^{(p)} = C\) (a constant), but constants are already in the homogeneous solution. So we “bump up” by one degree and try \(E_k^{(p)} = Ck\) — but linear functions are also in the homogeneous solution (because of the double root). So we bump up again and try \(E_k^{(p)} = Ck^2\). Substituting:
$$ C(k+1)^2 - 2Ck^2 + C(k-1)^2 = C\bigl[(k^2 + 2k + 1) - 2k^2 + (k^2 - 2k + 1)\bigr] = 2C. $$We need \(2C = -2\), so \(C = -1\). The particular solution is \(-k^2\).
(The rule: if the forcing term’s natural form is already a solution of the homogeneous equation, multiply by \(k\) until it isn’t. This is identical to the “multiply by \(x\)” rule for undetermined coefficients in ODEs.)
Step 5: Apply boundaries. General solution: \(E_k = A + Bk - k^2\).
$$ \begin{aligned} E_0 = 0 &\implies A = 0, \\ E_n = 0 &\implies Bn - n^2 = 0 \implies B = n. \end{aligned} $$Therefore:
$$ E_k = nk - k^2 = k(n - k). \quad\square $$| \(k\) | \(E_k\) for \(n = 10\) | Shape |
|---|---|---|
| 0 | 0 | Already at barrier |
| 1 | 9 | |
| 3 | 21 | |
| 5 | 25 | Maximum (midpoint) |
| 7 | 21 | Symmetric |
| 9 | 9 | |
| 10 | 0 | Already at barrier |
The parabola peaks at \(k = n/2\) with maximum \(E_{n/2} = n^2/4\). For \(k = 50, n = 100\): the expected duration is \(\mathbf{2{,}500}\) flips. The game is much longer than most people guess.
Sub-skill learned: the \(+1\) forcing term distinguishes expected-value recurrences from probability recurrences. The recipe: characteristic equation for the homogeneous part, then guess a particular solution matching the forcing term’s form (constant forcing → try quadratic when lower degrees are taken).
5b. Expected Flips to See HHH
You flip a fair coin repeatedly. What is the expected number of flips until you first see three consecutive heads (HHH)?
Before reading on: is the answer closer to 6, 8, 14, or 20?
Why single-state recurrences fail
The gambler’s ruin has a natural single-variable state: the current wealth \(k\). Here there is no single number that captures your progress. You might have flipped 100 coins, but all that matters for the future is how many consecutive heads you currently have. This is the state — and it takes three distinct values, giving a system of equations instead of a single recurrence.
Define states by progress toward the pattern. For HHH, the state is the length of your current consecutive-heads streak: \(S_0\) (streak = 0), \(S_1\) (streak = 1), \(S_2\) (streak = 2). A third H from \(S_2\) completes the pattern. Each state gets its own expected-value unknown, giving a system of three coupled equations.
Step 1: State transitions. Draw the automaton:
- \(S_0\): flip H \(\to S_1\), flip T \(\to S_0\)
- \(S_1\): flip H \(\to S_2\), flip T \(\to S_0\) (one head lost — full reset)
- \(S_2\): flip H \(\to\) DONE, flip T \(\to S_0\) (two heads lost — full reset)
Step 2: Write the system. Let \(E_i\) = expected flips from state \(S_i\):
$$ \begin{aligned} E_0 &= 1 + \tfrac{1}{2}\,E_1 + \tfrac{1}{2}\,E_0 &\quad&(1) \\ E_1 &= 1 + \tfrac{1}{2}\,E_2 + \tfrac{1}{2}\,E_0 &\quad&(2) \\ E_2 &= 1 + \tfrac{1}{2}\cdot 0 + \tfrac{1}{2}\,E_0 &\quad&(3) \end{aligned} $$Step 3: Solve by substitution.
From (1): \(\tfrac{1}{2}E_0 = 1 + \tfrac{1}{2}E_1\), so \(E_0 = 2 + E_1\). …(i)
From (3): \(E_2 = 1 + \tfrac{1}{2}E_0\). …(iii)
Substitute (iii) into (2):
$$ E_1 = 1 + \tfrac{1}{2}\!\left(1 + \tfrac{1}{2}E_0\right) + \tfrac{1}{2}E_0 = \tfrac{3}{2} + \tfrac{3}{4}E_0. \quad\ldots\text{(iv)} $$Substitute (iv) into (i):
$$ E_0 = 2 + \tfrac{3}{2} + \tfrac{3}{4}E_0 = \tfrac{7}{2} + \tfrac{3}{4}E_0. $$Solving: \(\tfrac{1}{4}E_0 = \tfrac{7}{2}\), so \(\mathbf{E_0 = 14}\).
Step 4: Back-substitute. \(E_2 = 1 + 7 = 8\). \(E_1 = \tfrac{3}{2} + \tfrac{21}{2} = 12\).
Verify equation (2): \(1 + \tfrac{1}{2}(8) + \tfrac{1}{2}(14) = 1 + 4 + 7 = 12\). \(\checkmark\) \(\square\)
The answer is surprisingly large. Any time you are building a streak of heads and a tail appears, you lose all your progress — you are thrown back to \(S_0\) regardless of whether you had one head or two. This “reset to zero” penalty is severe.
| Pattern | \(\E[\text{first occurrence}]\) | Formula |
|---|---|---|
| H | 2 | \(2^2 - 2\) |
| HH | 6 | \(2^3 - 2\) |
| HHH | 14 | \(2^4 - 2\) |
| HHHH | 30 | \(2^5 - 2\) |
| \(\underbrace{HH\cdots H}_{k}\) | \(2^{k+1} - 2\) | Exponential growth |
The general formula \(2^{k+1} - 2\) for \(k\) consecutive heads follows from the same recurrence structure. The exponential growth reflects the devastating cost of full resets.
Sub-skill learned: when the state space has multiple states, write one equation per state. The system is always linear and solvable by substitution. The art is identifying the minimal set of states — here, the length of the current streak.
5c. The Drunkard’s Walk (No Upper Barrier)
A drunkard starts at position \(k\) on the number line (\(k \geq 1\)). Each step: \(+1\) with probability \(p\), \(-1\) with probability \(q = 1 - p\). Position \(0\) is a cliff (absorbing). There is no upper barrier. What is the probability the drunkard eventually falls off the cliff?
Before reading on: for \(p = 1/2\), does the drunkard eventually fall with certainty? What about \(p = 0.6\) — does a slight rightward bias save the drunkard?
Why the standard formula doesn’t directly apply
The gambler’s ruin formula \(p_k = (r^k - 1)/(r^n - 1)\) has two barriers. With no upper barrier, \(n = \infty\), and the formula becomes \(0/0\) or \(\infty/\infty\) depending on \(r\). We need to take the limit carefully — and the answer depends critically on whether \(r < 1\), \(r = 1\), or \(r > 1\).
This is gambler’s ruin in the limit \(n \to \infty\). The recurrence \(p_k = p \cdot p_{k+1} + q \cdot p_{k-1}\) is the same, but the boundary condition at \(n\) is replaced by the requirement that \(0 \leq p_k \leq 1\) for all \(k\). This boundedness constraint selects the physically meaningful solution from the family — and creates a phase transition at \(p = 1/2\).
Step 1: The recurrence. Let \(p_k = \Pr[\text{eventually reach } 0 \mid \text{start at } k]\). Then:
$$ p_k = p \cdot p_{k+1} + q \cdot p_{k-1}, \qquad k \geq 1. $$Boundary: \(p_0 = 1\). No upper boundary — instead, require \(0 \leq p_k \leq 1\) for all \(k\).
Step 2: General solution via the characteristic equation. Rearrange the recurrence: \(p \cdot p_{k+1} - p_k + q \cdot p_{k-1} = 0\). Guess \(p_k = x^k\) and substitute:
$$ p \cdot x^{k+1} - x^k + q \cdot x^{k-1} = 0 \quad\implies\quad x^{k-1}(p \cdot x^2 - x + q) = 0. $$The characteristic equation is \(p \cdot x^2 - x + q = 0\). Using \(q = 1 - p\):
$$ p \cdot x^2 - x + (1 - p) = 0 \quad\implies\quad (x - 1)(px - q) = 0. $$Roots: \(x_1 = 1\) and \(x_2 = q/p\). When \(p \neq 1/2\), these are distinct, so the general solution is \(A \cdot 1^k + B \cdot (q/p)^k\):
$$ p_k = A + B \cdot r^k, \qquad r = \frac{q}{p}. $$From \(p_0 = 1\): \(A + B = 1\), so \(p_k = (1 - B) + B \cdot r^k\).
Step 3: Case \(p \leq 1/2\) (so \(r = q/p \geq 1\)). If \(r > 1\) and \(B \neq 0\), then \(B \cdot r^k \to \pm\infty\) as \(k \to \infty\), making \(p_k\) leave \([0, 1]\). The only bounded solution is \(B = 0\), giving:
$$ p_k = 1 \quad\text{for all } k. \qquad\text{(Certain ruin.)} $$For \(p = 1/2\) (\(r = 1\)), the characteristic equation has a double root. The general solution is \(p_k = A + Bk\). Boundedness forces \(B = 0\), again giving \(p_k = 1\).
Step 4: Case \(p > 1/2\) (so \(r = q/p < 1\)). Now \(r^k \to 0\) as \(k \to \infty\), so \(B \cdot r^k\) stays bounded for any \(B\). Both \(p_k = 1\) (taking \(B = 0\)) and \(p_k = r^k\) (taking \(B = 1, A = 0\)) are valid bounded solutions satisfying \(p_0 = 1\). Which one is the ruin probability?
The answer comes from the finite problem. With an upper barrier at \(n\), we proved \(p_k = (r^n - r^k)/(r^n - 1)\). As \(n \to \infty\) with \(r < 1\): \(r^n \to 0\), so:
$$ p_k = \frac{0 - r^k}{0 - 1} = r^k = \left(\frac{q}{p}\right)^k < 1. \qquad\text{(Survival is possible.)} $$The limit selects \(A = 0, B = 1\). The solution \(p_k = 1\) is mathematically valid but physically wrong — it corresponds to the drunkard falling off a cliff that isn’t there (the upper barrier at \(\infty\)).
Step 5: Verify the \(r \geq 1\) case via the finite limit. For \(r \geq 1\), we claimed \(p_k = 1\). Check: from the finite formula \(p_k = (r^n - r^k)/(r^n - 1)\) with \(r > 1\), as \(n \to \infty\): \(r^n \to \infty\), so \(p_k \to r^n / r^n = 1\). For \(r = 1\): \(p_k = (n - k)/(n - 1) \to 1\). Both match. \(\square\)
At \(p = 1/2\), the walk is recurrent — it returns to every point infinitely often, and ruin is certain no matter how far from the cliff you start. At any \(p > 1/2\), the walk is transient — it drifts rightward and has a positive probability of never returning.
| \(p\) | \(r = q/p\) | \(\Pr[\text{ruin}]\) from \(k = 3\) | Interpretation |
|---|---|---|---|
| \(0.50\) | \(1.00\) | \(1.000\) | Certain ruin (recurrent) |
| \(0.51\) | \(0.961\) | \(0.888\) | Slight bias barely helps |
| \(0.60\) | \(0.667\) | \(0.296\) | Strong bias gives 70% survival |
| \(0.70\) | \(0.429\) | \(0.079\) | 92% chance of escaping forever |
| \(0.90\) | \(0.111\) | \(0.001\) | Near-certain survival |
Sub-skill learned: when the state space is infinite, the recurrence has multiple solutions and the physics of the problem (boundedness, requiring \(0 \leq p_k \leq 1\)) selects the correct one. Always check: does your solution stay in \([0,1]\)? Does \(r^k\) blow up or decay?
5d. Expected Flips to See HTH
You flip a fair coin repeatedly. What is the expected number of flips until you first see the pattern H, T, H in sequence?
Before reading on: you just solved \(\E[\text{HHH}] = 14\). Is \(\E[\text{HTH}]\) larger, smaller, or equal? All three-flip patterns have the same probability (\(1/8\)) of appearing in any given window of three consecutive flips. Does that mean they all have the same expected waiting time?
The trap: equal probability does not mean equal waiting time
Every specific three-letter pattern (HHH, HHT, HTH, HTT, THH, THT, TTH, TTT) has probability \(1/8\) of appearing in any given window of three flips. The naive conclusion is that all patterns should have the same expected waiting time. This is wrong, and the reason is subtle: different patterns have different overlap structures. When you fail to complete a pattern, the amount of progress you retain depends on the pattern — and this changes the recurrence.
Build the state automaton for HTH. The states are the longest suffix of the flips seen so far that matches a prefix of the target HTH:
- \(S_0\) = no prefix matched (empty or last flip was T without prior context)
- \(S_1\) = matched “H” (the first character)
- \(S_2\) = matched “HT” (the first two characters)
The crucial difference from HHH: from \(S_1\), flipping H stays in \(S_1\). The sequence “HH” has suffix “H”, which is a prefix of HTH — so you retain partial progress instead of resetting to zero. This self-loop changes the recurrence and changes the answer.
Step 1: State transitions for HTH.
- \(S_0\): flip H \(\to S_1\), flip T \(\to S_0\)
- \(S_1\): flip H \(\to S_1\) (self-loop: “HH” has suffix “H”), flip T \(\to S_2\)
- \(S_2\): flip H \(\to\) DONE, flip T \(\to S_0\) (“HTT” has no matching prefix)
Step 2: Write the system.
$$ \begin{aligned} E_0 &= 1 + \tfrac{1}{2}\,E_1 + \tfrac{1}{2}\,E_0 &\quad&(1) \\ E_1 &= 1 + \tfrac{1}{2}\,E_1 + \tfrac{1}{2}\,E_2 &\quad&(2) \\ E_2 &= 1 + \tfrac{1}{2}\cdot 0 + \tfrac{1}{2}\,E_0 &\quad&(3) \end{aligned} $$Compare with HHH: the only difference is equation (2). In HHH, tails from \(S_1\) resets to \(S_0\), so equation (2) has \(\tfrac{1}{2}E_0\). Here, heads from \(S_1\) stays at \(S_1\), so equation (2) has \(\tfrac{1}{2}E_1\).
Step 3: Solve.
From (1): \(\tfrac{1}{2}E_0 = 1 + \tfrac{1}{2}E_1\), so \(E_0 = 2 + E_1\). …(i)
From (2): \(\tfrac{1}{2}E_1 = 1 + \tfrac{1}{2}E_2\), so \(E_1 = 2 + E_2\). …(ii)
From (3): \(E_2 = 1 + \tfrac{1}{2}E_0\). …(iii)
Substitute (iii) into (ii): \(E_1 = 2 + 1 + \tfrac{1}{2}E_0 = 3 + \tfrac{1}{2}E_0\).
Substitute into (i): \(E_0 = 2 + 3 + \tfrac{1}{2}E_0 = 5 + \tfrac{1}{2}E_0\).
Solving: \(\tfrac{1}{2}E_0 = 5\), so \(\mathbf{E_0 = 10}\).
Step 4: Back-substitute and verify. \(E_2 = 1 + 5 = 6\). \(E_1 = 2 + 6 = 8\).
Check (2): \(1 + \tfrac{1}{2}(8) + \tfrac{1}{2}(6) = 1 + 4 + 3 = 8\). \(\checkmark\) \(\square\)
| HHH | HTH | |
|---|---|---|
| Equation (1) | \(E_0 = 1 + \tfrac{1}{2}E_1 + \tfrac{1}{2}E_0\) | Same |
| Equation (2) | \(E_1 = 1 + \tfrac{1}{2}E_2 + \mathbf{\tfrac{1}{2}E_0}\) | \(E_1 = 1 + \mathbf{\tfrac{1}{2}E_1} + \tfrac{1}{2}E_2\) |
| Equation (3) | \(E_2 = 1 + \tfrac{1}{2}(0) + \tfrac{1}{2}E_0\) | Same |
| Answer | \(\mathbf{14}\) | \(\mathbf{10}\) |
The only structural difference is equation (2). In HHH, tails from \(S_1\) causes a full reset (\(E_0\)). In HTH, heads from \(S_1\) is a self-loop (\(E_1\)) — partial progress is preserved. This single difference reduces the expected time from 14 to 10.
| Pattern | \(\E[\text{first occurrence}]\) | Key feature |
|---|---|---|
| H | 2 | Single flip |
| HH | 6 | Full reset on T |
| HT | 4 | H keeps partial progress |
| HHH | 14 | Full reset on T from any state |
| HTH | 10 | Self-loop preserves progress |
| THH | 8 | First T is easy, then HH |
| HTT | 8 | First H is easy, then TT |
Self-overlapping patterns (like HHH, where a suffix equals a prefix) take longer because failure destroys more progress. Non-overlapping patterns complete faster.
Sub-skill learned: the state space for a pattern-matching problem is the automaton of the pattern — the states are prefixes of the target, and transitions depend on the overlap structure. Different patterns produce different automata, different recurrences, and different answers. The art is building the automaton correctly: for each state and each flip outcome, find the longest suffix of the new sequence that matches a prefix of the target.
The Recurrence Relation Playbook
Five patterns that cover every first-step-analysis problem you’ll face.
(1) Define \(f(\text{state})\) = the quantity of interest (probability, expected value, expected cost).
(2) Condition on the first transition out of the current state.
(3) Write the recurrence: \(f(\text{state}) = \sum_{\text{transitions}} [\text{prob} \times f(\text{next state})] + \text{cost}\).
(4) Identify boundary conditions (absorbing states).
(5) Solve.
The “cost” is \(0\) for probability recurrences (homogeneous) and \(1\) for expected-value recurrences (non-homogeneous). This single difference changes the solution method entirely.
| Probability recurrence | Expected-value recurrence | |
|---|---|---|
| Form | \(p_k = \tfrac{1}{2}p_{k+1} + \tfrac{1}{2}p_{k-1}\) | \(E_k = \mathbf{1} + \tfrac{1}{2}E_{k+1} + \tfrac{1}{2}E_{k-1}\) |
| Type | Homogeneous | Non-homogeneous |
| Method | Try \(p_k = r^k\), solve characteristic eq | Particular + homogeneous |
| Fair coin | \(p_k = k/n\) (linear) | \(E_k = k(n - k)\) (quadratic) |
Know both cold. Interviewers will often follow a probability question with “now what about the expected duration?” and expect you to adapt the recurrence on the fly.
Many problems have a state space that isn’t a simple number line. The art is choosing the minimal state that satisfies the Markov property: the future depends only on the current state, not the history.
| Problem | State |
|---|---|
| Gambler’s ruin | Current wealth \(k\) |
| Consecutive pattern (HHH) | Current streak length (0, 1, 2) |
| Non-consecutive pattern (HTH) | Longest matching prefix (automaton state) |
| Multi-player game | (score_A, score_B) or (advantage, turns left) |
If you find yourself conditioning on more and more history, your state space is too small. If your system has 10+ equations, look for symmetries that collapse states.
When you have \(n\) states, you get \(n\) linear equations in \(n\) unknowns. For interview problems, \(n\) is usually 2–4. The approach:
(1) Write all equations.
(2) Find equations with only one unknown beyond a “base” unknown (usually \(E_0\)).
(3) Substitute to express everything in terms of \(E_0\).
(4) Solve for \(E_0\).
(5) Back-substitute to find the remaining unknowns, then verify against the original equations.
If \(X_t\) is a martingale and \(T\) is a stopping time, then \(\E[X_T] = \E[X_0]\) (optional stopping theorem). This can bypass recurrences entirely:
Ruin probability: wealth \(X_t\) is a martingale (fair coin). \(\E[X_T] = k\). Since \(X_T \in \{0, n\}\): \(n \cdot P(\text{win}) = k\), so \(P(\text{win}) = k/n\). No recurrence needed.
Expected duration: \(X_t^2 - t\) is also a martingale. \(\E[X_T^2 - T] = k^2\). Since \(X_T^2 \in \{0, n^2\}\): \(n^2(k/n) - \E[T] = k^2\), giving \(\E[T] = k(n - k)\). Both answers in two lines.
Biased coin: \((q/p)^{X_t}\) is a martingale. Apply optional stopping to recover the biased ruin probability immediately.
Biased duration: for the biased gambler’s ruin (\(p \neq q\)), the expected duration from position \(k\) to absorption at 0 or \(n\) is:
$$ D_k = \frac{k}{q - p} - \frac{n}{q - p} \cdot \frac{1 - (q/p)^k}{1 - (q/p)^n}. $$Derivation: solve the non-homogeneous recurrence \(D_k = pD_{k+1} + qD_{k-1} + 1\) with \(D_0 = D_n = 0\). The homogeneous part has roots \(1\) and \(q/p\); the particular solution is \(k/(q - p)\). Boundary conditions determine the constants. As \(p \to 1/2\), this degenerates to \(D_k = k(n - k)\) (L’Hôpital or direct limit). \(\checkmark\)
Use the martingale shortcut when you can identify the right martingale. Use recurrences when you cannot — they always work, even when no convenient martingale exists.
For the full theory — definitions, proofs, trading connections, and interview games — see Martingales from Scratch below.
| Problem | State space | Recurrence type | Answer |
|---|---|---|---|
| Ruin prob (fair) | \(k \in \{0, \ldots, n\}\) | Homogeneous, single-state | \(k/n\) |
| Ruin prob (biased) | \(k \in \{0, \ldots, n\}\) | Homogeneous, single-state | \((r^k - 1)/(r^n - 1)\) |
| Expected duration (fair) | \(k \in \{0, \ldots, n\}\) | Non-homogeneous, single-state | \(k(n - k)\) |
| Expected duration (biased) | \(k \in \{0, \ldots, n\}\) | Non-homogeneous, single-state | \(\displaystyle\frac{k}{q - p} - \frac{n}{q - p} \cdot \frac{1 - (q/p)^k}{1 - (q/p)^n}\) |
| Infinite walk ruin | \(k \in \{1, 2, \ldots\}\) | Homogeneous, infinite state | \(1\) or \((q/p)^k\) |
| \(\E[\text{HHH}]\) | \(\{S_0, S_1, S_2\}\) | Non-homog, 3-state system | 14 |
| \(\E[\text{HTH}]\) | \(\{S_0, S_1, S_2\}\) | Non-homog, 3-state system | 10 |
Martingales from Scratch
In the Recurrence Relation Playbook, we used the “martingale shortcut” to solve the gambler’s ruin in two lines — no recurrences needed. But we never explained where those magical formulas came from. Why \(S_t^2 - t\)? Why \((q/p)^{S_t}\)? How would you ever discover these yourself, sitting in a Jane Street interview with nothing but a marker and a whiteboard?
This section answers that question. We build martingale theory from a single idea, show exactly how to find martingales (not just verify them), and then use them to demolish interview games.
The Big Idea: Conserved Quantities
In physics, a roller coaster changes height and speed every instant, but one quantity never changes: total energy = kinetic + potential. If you know the energy at the start, you know it everywhere. You don’t need to track the cart’s position at every moment — the conservation law lets you jump straight from start to finish.
Probability has exactly the same idea. A random process changes every step — a gambler’s wealth goes up and down, a particle drifts left and right. But sometimes there is a function of the state that stays the same on average. Such a function is called a martingale.
Let’s see this with the simplest possible game.
Fair coin. You start with \(\$k\). Each flip: heads you gain \(\$1\), tails you lose \(\$1\). You play until you reach \(\$n\) (win) or \(\$0\) (ruin).
Let \(S_t\) = your wealth after \(t\) flips. Let’s ask: what is the average wealth next round, given that you currently have \(\$x\)?
$$ \E[S_{t+1} \mid S_t = x] = \frac{1}{2}(x + 1) + \frac{1}{2}(x - 1) = x. $$The answer is just \(x\) — your current wealth. On average, your wealth doesn’t change. The game is fair: the coin has no bias, so your expected wealth tomorrow equals your wealth today.
This is a conserved quantity. No matter how long you play, no matter what wild sequence of heads and tails you see, the average value of \(S_t\) stays put. And that gives us a direct line to the answer:
If wealth is conserved on average, then the average wealth at the end of the game equals the wealth at the start:
$$ \E[S_T] = S_0 = k. $$The game ends at \(S_T \in \{0, n\}\). Let \(w = \Pr(\text{win})\). Then:
$$ n \cdot w + 0 \cdot (1 - w) = k \implies w = \frac{k}{n}. $$Ruin probability in one line. No recurrence, no characteristic equation, no algebra. Just conservation.
That is the entire idea of martingales. Everything that follows is about (a) making this rigorous, and (b) learning to discover the right conserved quantity for any problem.
A sequence of random variables \(M_0, M_1, M_2, \ldots\) is a martingale if for every \(t \geq 0\):
(1) \(\E[|M_t|] < \infty\) (each value is finite on average), and
(2) \(\E[M_{t+1} \mid M_0, M_1, \ldots, M_t] = M_t\) (the expected next value, given the full history, equals the current value).
In plain English: the best prediction of the future is the present.
Two relatives show up often:
A sub-martingale has \(\E[M_{t+1} \mid \text{past}] \geq M_t\) — a favorable game (expected to go up).
A super-martingale has \(\E[M_{t+1} \mid \text{past}] \leq M_t\) — an unfavorable game (expected to go down).
The word “martingale” comes from 18th-century French gambling — the “martingale strategy” was the doubling system (bet 1, lose, bet 2, lose, bet 4, …). Every gambler who tried it went broke. Paul Lévy formalized the mathematical concept in 1934; Joseph Doob built the full theory in the 1940s–50s, proving the convergence and stopping theorems that make martingales one of the most powerful tools in probability.
The One Question That Finds Every Martingale
Here is the secret that textbooks bury under formalism. Every martingale discovery reduces to answering one question:
Given a random process \(S_t\), find a function \(f\) such that:
$$ \E[f(S_{t+1}) \mid S_t = x] = f(x). $$If you find such an \(f\), then \(M_t = f(S_t)\) is a martingale.
That’s it. The entire “discovery” process is: what function of the state has zero expected change? Once you frame it this way, finding martingales becomes algebra, not inspiration. You don’t need to memorize formulas. You just solve an equation.
For a ±1 random walk with probabilities \(p\) (up) and \(q = 1 - p\) (down), the discovery question becomes:
$$ p \cdot f(x + 1) + q \cdot f(x - 1) = f(x). $$This is a functional equation. Different choices of \(f\) give different martingales, each useful for a different question. Here are the three strategies that cover 90% of interview problems:
Check: \(p(x + 1) + q(x - 1) = px + p + qx - q = x + (p - q)\).
This equals \(f(x) = x\) only when \(p = q = 1/2\) — a fair game.
When it works: the game has zero drift (fair coin, fair bets).
What it solves: ruin/hitting probabilities (“what fraction of the time do you reach target \(n\)?”).
When you need expected duration, you need a martingale that involves time \(t\). Try \(M_t = g(S_t) - t\). For this to be a martingale, you need \(\E[g(S_{t+1}) \mid S_t = x] = g(x) + 1\) (the \(g\) part must go up by 1 on average each step, cancelling the \(-1\) from the time part).
When it works: whenever you need expected time to reach a boundary.
What it solves: expected duration, expected hitting times.
Check: \(p \cdot r^{x+1} + q \cdot r^{x-1} = r^x\). Divide both sides by \(r^{x-1}\): \(pr^2 + q = r\), i.e., \(pr^2 - r + q = 0\). Solve for \(r\).
When it works: biased games (\(p \neq q\)) where Strategy A fails.
What it solves: ruin probabilities in biased random walks.
Notice the pattern. You pick an ansatz (a guess for the form of \(f\)), plug it into the functional equation, and solve for the parameters. This is exactly like guessing \(x^k\) as a solution to a recurrence — but now you’re building a conserved quantity, not solving a recurrence directly.
There’s no magic. There’s no divine inspiration. There is a functional equation and three strategies to try. Let’s see each one in full detail.
Three Worked Discoveries
For each discovery below, we start with a question we want to answer, then show how the martingale emerges naturally from solving the functional equation.
Question: fair coin, start at \(\$k\), barriers at \(0\) and \(n\). Probability of reaching \(n\)?
Step 1: Write the functional equation. Fair coin means \(p = q = 1/2\). We want \(f\) with:
$$ \frac{1}{2} f(x + 1) + \frac{1}{2} f(x - 1) = f(x). $$Step 2: Try Strategy A. \(f(x) = x\):
$$ \frac{1}{2}(x + 1) + \frac{1}{2}(x - 1) = x. \quad \checkmark $$It works! So \(M_t = S_t\) (wealth itself) is a martingale.
Step 3: Extract the answer. Since \(\E[M_T] = \E[M_0]\):
$$ \E[S_T] = k. \quad \text{But } S_T \in \{0, n\}, \text{ so } n \cdot \Pr(\text{win}) = k \implies \Pr(\text{win}) = \frac{k}{n}. \quad\square $$That was almost too easy. The next one is the one everyone finds mysterious.
Question: same fair-coin game. What is the expected number of flips until the game ends?
Step 1: What do we need? We need a martingale that involves time \(t\), because we want \(\E[T]\). Try the form:
$$ M_t = g(S_t) - t. $$If this is a martingale, then at the stopping time \(T\): \(\E[g(S_T) - T] = g(S_0) - 0 = g(k)\). This gives \(\E[T] = \E[g(S_T)] - g(k)\) — and we can compute \(\E[g(S_T)]\) because \(S_T \in \{0, n\}\). So our goal is to find \(g\).
Step 2: Write the functional equation. For \(g(S_t) - t\) to be a martingale:
$$ \E[g(S_{t+1}) - (t+1) \mid S_t = x] = g(x) - t. $$The \(-t\) cancels, leaving:
$$ \E[g(S_{t+1}) \mid S_t = x] = g(x) + 1. $$In words: \(g\) must go up by exactly 1, on average, each step. For the fair ±1 walk:
$$ \frac{1}{2}\, g(x + 1) + \frac{1}{2}\, g(x - 1) = g(x) + 1. \tag{$\star$} $$Step 3: Solve for \(g\). Try a polynomial. The simplest guess is \(g(x) = ax^2 + bx + c\). Plug into (\(\star\)):
$$ \begin{aligned} \text{LHS} &= \frac{1}{2}\bigl[a(x+1)^2 + b(x+1) + c\bigr] + \frac{1}{2}\bigl[a(x-1)^2 + b(x-1) + c\bigr] \\[4pt] &= \frac{1}{2}\bigl[ax^2 + 2ax + a + bx + b + c\bigr] + \frac{1}{2}\bigl[ax^2 - 2ax + a + bx - b + c\bigr] \\[4pt] &= ax^2 + a + bx + c. \end{aligned} $$ $$ \text{RHS} = ax^2 + bx + c + 1. $$Setting LHS = RHS: \(ax^2 + a + bx + c = ax^2 + bx + c + 1\), which gives \(a = 1\). The parameters \(b\) and \(c\) can be anything — take \(b = c = 0\) for simplicity.
So \(g(x) = x^2\), and
$$ \boxed{M_t = S_t^2 - t \text{ is a martingale.}} $$Step 4: Extract the answer. Apply \(\E[M_T] = \E[M_0]\):
$$ \E[S_T^2 - T] = S_0^2 - 0 = k^2. $$Since \(S_T \in \{0, n\}\) and \(\Pr(S_T = n) = k/n\) (from Discovery 1):
$$ \E[S_T^2] = n^2 \cdot \frac{k}{n} + 0^2 \cdot \frac{n - k}{n} = nk. $$Therefore:
$$ \E[T] = \E[S_T^2] - k^2 = nk - k^2 = k(n - k). \quad\square $$\(S_t^2 - t\) didn’t fall from the sky. We asked for a function \(g\) satisfying \(\tfrac{1}{2}g(x+1) + \tfrac{1}{2}g(x-1) = g(x) + 1\), tried a quadratic, and \(g(x) = x^2\) popped out. If you remember one thing from this section, remember this: martingales are solutions to functional equations, not things you memorize.
Question: biased coin (\(\Pr(\text{heads}) = p \neq 1/2\)). Start at \(k\), barriers at \(0\) and \(n\). Probability of reaching \(n\)?
Step 1: Why Strategy A fails. Try \(f(x) = x\):
$$ p(x + 1) + q(x - 1) = x + (p - q) \neq x. $$Wealth is NOT conserved. The coin has a drift of \(p - q\) per flip. We could subtract the drift (\(S_t - (p-q)t\) is a martingale), but that’s useless for ruin: the boundaries are at fixed positions \(0\) and \(n\), but this martingale involves time, which we don’t know at the boundaries.
Step 2: Use Strategy C. We need a function of position alone. Try \(f(x) = r^x\) for some unknown base \(r\).
Plug into the functional equation \(p \cdot f(x+1) + q \cdot f(x-1) = f(x)\):
$$ p \cdot r^{x+1} + q \cdot r^{x-1} = r^x. $$Step 3: Solve for \(r\). Divide everything by \(r^{x-1}\):
$$ p r^2 + q = r \implies p r^2 - r + q = 0. $$This is a quadratic in \(r\). Apply the quadratic formula:
$$ r = \frac{1 \pm \sqrt{1 - 4pq}}{2p}. $$Now use the identity \(1 - 4pq = (p + q)^2 - 4pq = (p - q)^2\), so \(\sqrt{1 - 4pq} = |p - q|\). The two roots are:
$$ r = \frac{1 + (p - q)}{2p} = \frac{2p}{2p} = 1, \qquad r = \frac{1 - (p - q)}{2p} = \frac{2q}{2p} = \frac{q}{p}. $$Root \(r = 1\) gives \(f(x) = 1^x = 1\) — a constant. True but useless. The interesting root is \(r = q/p\), giving:
$$ \boxed{M_t = \left(\frac{q}{p}\right)^{S_t} \text{ is a martingale.}} $$Step 4: Verify (one line). Check that \(p \cdot r + q/r = 1\):
$$ p \cdot \frac{q}{p} + q \cdot \frac{p}{q} = q + p = 1. \quad\checkmark $$Step 5: Extract the answer. Let \(r = q/p\). Apply \(\E[M_T] = \E[M_0]\):
$$ \E[r^{S_T}] = r^{S_0} = r^k. $$Since \(S_T \in \{0, n\}\), with \(w = \Pr(S_T = n)\):
$$ r^n \cdot w + r^0 \cdot (1 - w) = r^k \implies w = \frac{r^k - 1}{r^n - 1}. $$This is the biased gambler’s ruin formula — found by solving a quadratic. No recurrence, no characteristic equation for the recurrence. Just one functional equation and Strategy C. \(\quad\square\)
All three discoveries followed the same three steps:
(1) Decide what you want to compute (ruin probability? expected duration?).
(2) Write the functional equation \(\E[f(S_{t+1}) \mid S_t = x] = f(x)\) and solve for \(f\) using an ansatz (linear, quadratic, exponential).
(3) Apply \(\E[f(S_T)] = f(S_0)\) and read off the answer from the boundary values.
The only question is which ansatz to try — and with three strategies in your pocket, you can try all of them in under a minute each.
But wait — we’ve been using a fact we haven’t proven: that \(\E[M_T] = \E[M_0]\) when you stop a martingale. This is true, but it has conditions. Let’s prove it.
The Optional Stopping Theorem
The claim we’ve been using — “a martingale’s expected value at a stopping time equals its starting value” — is the Optional Stopping Theorem (OST). It is the most important result in martingale theory for interviews. But it has conditions, and those conditions are a favorite interview trap.
First, what is a stopping time? It is a random time \(T\) where you can tell whether \(T = t\) by looking only at the history up to time \(t\), without peeking into the future. Examples:
• “The first time wealth hits 0” — yes, stopping time. At each moment, you can check “is my wealth 0 right now?”
• “The last time wealth hits 0” — no. You’d need to know the future to know it’s the last visit.
• “The first time I’ve seen 3 heads in a row” — yes. You can track the current streak.
Let \(M_t\) be a martingale and \(T\) a stopping time. If:
(a) \(\E[T] < \infty\) (the game ends in finite expected time), and
(b) \(|M_{t+1} - M_t| \leq C\) for some constant \(C\) (increments are bounded),
then
$$ \E[M_T] = \E[M_0]. $$In words: you can’t beat a fair game by choosing when to quit, as long as the bets are bounded and the game ends eventually.
Step 1: Telescope. Write the stopped value as a sum of increments:
$$ M_T - M_0 = \sum_{t=0}^{\infty} (M_{t+1} - M_t) \cdot \mathbf{1}_{\{T > t\}}. $$The indicator \(\mathbf{1}_{\{T > t\}}\) equals 1 while the game is running and 0 after you stop. So this sum picks up exactly the increments that happen before stopping.
Step 2: Each term has expectation zero. The key: \(\{T > t\}\) depends only on \(M_0, \ldots, M_t\) (because \(T\) is a stopping time — you can decide whether you’ve stopped by looking at the past). So \(\mathbf{1}_{\{T > t\}}\) is determined by the history up to time \(t\). This lets us use the tower property:
$$ \E\bigl[(M_{t+1} - M_t) \cdot \mathbf{1}_{\{T > t\}}\bigr] = \E\bigl[\underbrace{\E[M_{t+1} - M_t \mid \text{history up to } t]}_{= 0 \text{ (martingale!)}} \cdot\; \mathbf{1}_{\{T > t\}}\bigr] = 0. $$Step 3: Swap \(\E\) and \(\sum\). Can we pass the expectation inside the infinite sum? Yes, because:
$$ \sum_{t=0}^{\infty} \E\bigl[|M_{t+1} - M_t| \cdot \mathbf{1}_{\{T > t\}}\bigr] \leq C \sum_{t=0}^{\infty} \Pr(T > t) = C \cdot \E[T] < \infty. $$Bounded increments (\(\leq C\)) and finite expected stopping time (\(\E[T] < \infty\)) guarantee absolute convergence. So \(\E[M_T - M_0] = \sum_t 0 = 0\). \(\quad\square\)
With the OST proved, let’s summarize the three applications that we derived in the discoveries above — now on rigorous footing.
Martingale: \(S_t\). Increments bounded by 1. \(\E[T] < \infty\) (game between absorbing barriers). By OST: \(\E[S_T] = k\). Since \(S_T \in \{0, n\}\): \(\Pr(\text{win}) = k/n\).
Martingale: \(S_t^2 - t\). Increments: \(|S_{t+1}^2 - (t+1) - S_t^2 + t| = |2S_t X_{t+1} + 1 - 1| = |2S_t X_{t+1}| \leq 2n\) (wealth stays in \([0, n]\)). By OST: \(\E[S_T^2 - T] = k^2\), giving \(\E[T] = k(n - k)\).
Martingale: \((q/p)^{S_t}\). Increments bounded (since \(S_t \in [0, n]\), the martingale stays in \([\min(1, r^n), \max(1, r^n)]\)). By OST: \(\E[r^{S_T}] = r^k\). Since \(S_T \in \{0, n\}\): \(\Pr(\text{win}) = (r^k - 1)/(r^n - 1)\) where \(r = q/p\).
Spotting Martingales in Interview Games
In a quant interview, you won’t be told “use a martingale.” You’ll be handed a game and asked “is this fair?”, “what’s the expected payoff?”, or “what’s the optimal strategy?” Here is the systematic method for translating any game into a martingale problem.
(1) Write the wealth process. Define \(W_t\) = your wealth (or score, or position) after \(t\) rounds. Be explicit: what are the possible transitions?
(2) Compute the expected one-step change. Find \(\E[W_{t+1} - W_t \mid W_t = x]\). This is the drift.
(3) Classify:
• Drift = 0: wealth is a martingale. Apply OST directly.
• Drift = \(d \neq 0\): wealth has a bias. \(W_t - dt\) is a martingale (subtract the drift).
• Need expected time? Build \(g(W_t) - t\) by solving \(\E[g(W_{t+1}) \mid W_t = x] = g(x) + 1\).
• Biased walk? Try \(r^{W_t}\) and solve \(pr^2 - r + q = 0\) for \(r\).
(4) Check OST conditions. Are increments bounded? Is \(\E[T] < \infty\)? If not, OST may fail — say so explicitly.
(5) Apply OST and extract the answer. Set \(\E[M_T] = \E[M_0]\), plug in boundary values, solve.
Let’s run this protocol on four games you might face across the table from a Jane Street interviewer.
Setup: you pay $1 to flip a fair coin. Heads: you get $3 back. Tails: you get nothing. Is this game fair?
Step 1: \(W_t\) = wealth after \(t\) rounds.
Step 2: each round, you pay $1. Heads (prob 1/2): receive $3, net change = +$2. Tails (prob 1/2): receive $0, net change = −$1.
$$ \E[\Delta W] = \frac{1}{2}(+2) + \frac{1}{2}(-1) = +0.50. $$Step 3: drift = +0.50 > 0. The game is favorable, not fair. \(W_t\) is a sub-martingale (expected to go up). The adjusted process \(W_t - 0.5t\) is a martingale. After \(n\) rounds, expected profit is \(0.5n\).
Follow-up: what entry fee \(c\) makes the game fair? Need \(\E[\Delta W] = (1/2)(3 - c) + (1/2)(0 - c) = 3/2 - c = 0\), so \(c = \$1.50\).
Setup: fair coin. Bet $1 on the first flip. Every time you lose, double your bet. Stop as soon as you win. The first win recovers all losses plus $1 profit. Is this a guaranteed way to make money?
Step 1: after \(k\) consecutive losses, total bet = \(1 + 2 + \cdots + 2^{k-1} = 2^k - 1\). Next bet: \(2^k\).
Step 2: each individual bet is on a fair coin, so \(\E[\Delta W] = 0\) each round. Wealth \(W_t\) IS a martingale.
Step 3: on the first win (after \(k\) losses), you receive \(2 \cdot 2^k = 2^{k+1}\). Total invested: \(2^{k+1} - 1\). Net profit: $1. Guaranteed!
…or is it?
Step 4 (OST check): the stopping time \(T\) = first win has \(\E[T] = 2 < \infty\) (\(\text{Geometric}(1/2)\)). But the increments are not bounded. At round \(k\), the bet is \(2^k\), which grows without limit. Condition (b) of OST is violated.
What happens: with a finite bankroll \(B\), you can make at most \(m = \lfloor \log_2(B + 1) \rfloor\) bets. If you lose all \(m\) (probability \(1/2^m\)):
$$ \E[\text{payoff}] = \Bigl(1 - \frac{1}{2^m}\Bigr)(+1) + \frac{1}{2^m}\bigl(-(2^m - 1)\bigr) = 0. $$Expected profit is exactly zero. The strategy doesn’t create value — it redistributes risk: high probability of a small win ($1), tiny probability of a catastrophic loss (all your money). This is the textbook example of why OST conditions matter: drop the bounded-increments condition and conservation breaks.
Setup: flip a fair coin until the first heads on flip \(k\). You win \(2^k\) dollars. How much should you pay to play?
Step 2 (expected payout):
$$ \E[\text{payout}] = \sum_{k=1}^{\infty} \frac{1}{2^k} \cdot 2^k = \sum_{k=1}^{\infty} 1 = \infty. $$Expected value is infinite! You should mortgage your house to play — right?
But if asked “how much would you actually pay?”, most people say $10–$20. The disconnect is real.
Resolution (Bernoulli, 1738): people maximize expected utility, not expected dollars. With logarithmic utility \(u(x) = \ln x\):
$$ \E[\ln(\text{payout})] = \sum_{k=1}^{\infty} \frac{k \ln 2}{2^k} = (\ln 2) \sum_{k=1}^{\infty} \frac{k}{2^k} = (\ln 2) \cdot 2 = 2\ln 2. $$(The series \(\sum k/2^k = 2\) follows from differentiating the geometric series \(\sum x^k = 1/(1-x)\) to get \(\sum kx^{k-1} = 1/(1-x)^2\), then setting \(x = 1/2\).)
Fair price: \(\ln c = 2\ln 2 \implies c = 4\). Under log utility, pay at most ~$4.
Martingale perspective: the payout process isn’t even integrable (\(\E[|\text{payout}|] = \infty\)), so it violates condition (1) of the martingale definition. It’s not a martingale. OST doesn’t apply. This is consistent: infinite expected value but no finite fair price.
Interview insight: this game tests whether you can recognize when martingale machinery breaks down. The right answer isn’t a number — it’s identifying that the expected value is infinite and pivoting to utility theory.
Setup: roll a fair die. You can keep the result as your payoff (in dollars), or pay $1 to re-roll. You can re-roll as many times as you want. What’s the optimal strategy and expected payoff?
Step 1: Define the value. Let \(V\) = expected payoff under optimal play. On any roll, if you see \(x\):
• Keep: you get \(x\).
• Re-roll: you pay $1 and get expected value \(V\) from the fresh start. Net value: \(V - 1\).
Optimal strategy: keep if \(x \geq V\), re-roll if \(x < V\).
Step 2: Self-consistency. \(V\) must satisfy \(V = \E[\max(X, V - 1)]\) where \(X\) is a die roll. We guess a threshold \(t\) (keep if \(X \geq t\), re-roll if \(X < t\)) and check for consistency.
Attempt 1: threshold = 5 (keep on 5 or 6, re-roll on 1–4).
$$ V = \frac{4}{6}(V - 1) + \frac{1}{6}(5) + \frac{1}{6}(6) = \frac{4}{6}V - \frac{4}{6} + \frac{11}{6}. $$ $$ \frac{2}{6}V = \frac{7}{6} \implies V = 3.5. $$But \(V = 3.5 < 4\), so we should also keep 4 (since \(4 > V\)). Our threshold was too high — we were re-rolling numbers we should have kept. Inconsistent.
Attempt 2: threshold = 4 (keep on 4, 5, or 6, re-roll on 1–3).
$$ V = \frac{3}{6}(V - 1) + \frac{1}{6}(4 + 5 + 6) = \frac{1}{2}V - \frac{1}{2} + \frac{15}{6} = \frac{1}{2}V + 2. $$ $$ \frac{1}{2}V = 2 \implies V = 4. $$Consistency check: \(V = 4\) = threshold. We keep \(\geq 4\), re-roll \(< 4\). Exactly what we assumed. \(\checkmark\)
Summary: optimal strategy is to keep 4, 5, or 6; re-roll 1, 2, or 3. Expected payoff: $4. Expected number of rolls: \(1/(3/6) = 2\) (geometric with success probability 1/2).
Martingale connection: define \(V_t\) = value of your position at time \(t\). Under optimal play, each re-roll costs $1 while expected value stays at \(V\), so \(\E[V_{t+1} \mid \mathcal{F}_t] \leq V_t\) — a super-martingale. You stop at the first time the reward exceeds the continuation value. This is the principle behind all optimal stopping problems: stop the first time the current reward beats the expected future.
Each game tested a different aspect of martingale thinking:
Game 1: can you compute drift and identify bias? (Most basic martingale check.)
Game 2: do you know when OST conditions fail? (The “free money” trap.)
Game 3: can you recognize when a process isn’t even a martingale? (Integrability failure.)
Game 4: can you set up an optimal stopping problem from scratch? (Self-consistency + threshold.)
An interviewer cycles through these to see if you truly understand the theory, or just memorized the formula \(\E[M_T] = \E[M_0]\).
A monkey sits at a typewriter and hits a uniformly random letter (from a 26-letter alphabet) each second. What is the expected number of keystrokes until the monkey types “ABRACADABRA”?
This is one of the most elegant applications of the optional stopping theorem. The idea: build a casino whose total payout is a martingale, then read off the answer from what the casino owes at the stopping time.
Setup. Before each keystroke, a fresh gambler walks in and bets $1 that the next letter will be “A” (the first letter of the target word). The odds are fair: 26-to-1 payout. If the gambler wins, they parlay their entire \($26\) on the next letter being “B” (again at 26:1 odds). If they win again, they bet everything on “R,” and so on, riding the streak as long as the letters match “ABRACADABRA.”
Why it’s a martingale. Each individual bet is fair (expected payout = stake). So the casino’s total outflow minus total inflow is a martingale. The casino takes in $1 per keystroke (one new gambler arrives). Let \(T\) be the stopping time (the word is completed). The total inflow is \(T\) dollars.
At time \(T\), who’s still alive? When “ABRACADABRA” first appears:
• The gambler who started 11 steps ago rode the entire word. Their stake grew: \($1 \to $26 \to $26^2 \to \cdots \to $26^{11}\). They hold \($26^{11}\).
• “ABRACADABRA” has “ABRA” as both a prefix and a suffix. So the gambler who started 4 steps ago saw “A-B-R-A” and is on a matching streak. They hold \($26^4\).
• The final “A” is also a prefix of length 1. The gambler who started 1 step ago matched “A.” They hold \($26^1\).
All other gamblers have busted out (hit a wrong letter).
Applying OST. The martingale is: (total casino outflow) \(-\) (total inflow) \(= 0\) in expectation. So:
$$ \E[\text{total payout}] = \E[\text{total inflow}] = \E[T]. $$The total payout at time \(T\) is the sum of all surviving gamblers’ holdings:
$$ \E[T] = 26^{11} + 26^4 + 26^1. $$That’s approximately \(3.67 \times 10^{15}\) keystrokes — an astronomically large number, dominated by the \(26^{11}\) term.
For any target word of length \(L\) over an alphabet of size \(k\):
$$ \E[T] = \sum_{\text{prefix-suffix overlaps}} k^{\text{overlap length}}. $$A “prefix-suffix overlap” means a string that is simultaneously a prefix and a suffix of the target word. Every word has at least the trivial overlap (the whole word itself, length \(L\)) and the single-character overlap (length 1, if the first and last letters match).
For a word with no overlaps (like “ABCDE”): \(\E[T] = k^L\). Overlaps make it harder — the monkey keeps getting “false starts” that reset progress.
Connection to Penney’s game: in a coin-flipping game where two players each choose a 3-letter pattern (like HHT vs THH), the pattern with more prefix-suffix overlaps has a higher expected waiting time — and therefore loses more often. This is the mathematical basis of the nontransitive trick in Penney’s game.
Martingales and Trading
Martingale theory was born in gambling, but it found its deepest application in finance. If you’re interviewing at a trading firm, this connection isn’t background — it’s the point.
If prices fully reflect all available information, then price changes are unpredictable. The price process \(P_t\) satisfies:
$$ \E[P_{t+1} \mid P_0, P_1, \ldots, P_t] = P_t. $$This is exactly the martingale property. EMH doesn’t say prices are random — it says you can’t consistently profit from the predictable part after transaction costs.
The deeper result: the Fundamental Theorem of Asset Pricing says a market is arbitrage-free if and only if there exists a probability measure under which discounted prices are martingales. This “risk-neutral measure” is the cornerstone of derivatives pricing. The Black–Scholes formula is, at its core, an expected value under this risk-neutral measure. The slogan: “no free lunch” = no arbitrage = prices are martingales.
A stock goes +20% one day, −20% the next. Arithmetic average return: \((+20\% - 20\%)/2 = 0\%\). Looks fair.
But your wealth: \(\$100 \to \$120 \to \$96\). You lost 4%.
This is volatility drag. In general:
$$ \text{geometric return} \approx \text{arithmetic return} - \frac{\sigma^2}{2}. $$Even when \(\E[S_{t+1}/S_t] = 1\) (arithmetic martingale), the geometric growth is negative: \(\E[\ln(S_{t+1}/S_t)] < 0\). The arithmetic game is fair, but your log-wealth drifts downward. This is why leveraged ETFs decay in sideways markets, and why “is this game fair?” requires you to ask: fair in what sense?
There are three levels of “fair,” from weakest to strongest:
| Level | Definition | Broken by |
|---|---|---|
| Per-round fair | \(\E[\Delta W_t] = 0\) each round | Doubling strategy: per-round fair but \(\E[W_T] \neq W_0\) with unbounded bets |
| Stopping-time fair | \(\E[W_T] = W_0\) for all stopping times | Geometric martingale: \(\E[W_T] = W_0\) but \(\E[\ln W_T] < \ln W_0\) (volatility drag) |
| Geometrically fair | \(\E[\ln(W_T / W_0)] = 0\) | (Strongest form — rare in practice) |
An interviewer who asks “is this fair?” is testing which level you default to. The right answer always starts with: “fair in which sense?”
The Martingale Playbook
Given a process \(S_t\) with transitions \(S_{t+1} = S_t + X_{t+1}\):
| You want | Try this ansatz | Functional equation | Solution |
|---|---|---|---|
| Hitting probability (fair) | \(f(x) = x\) | \(\tfrac{1}{2}f(x+1) + \tfrac{1}{2}f(x-1) = f(x)\) | \(f(x) = x\) works |
| Expected duration (fair) | \(g(x) - t\), try \(g(x) = x^2\) | \(\tfrac{1}{2}g(x+1) + \tfrac{1}{2}g(x-1) = g(x) + 1\) | \(g(x) = x^2\) works |
| Hitting probability (biased) | \(f(x) = r^x\) | \(pr^2 - r + q = 0\) | \(r = q/p\) |
| General drift removal | \(f(x) = x\), then subtract drift | \(\E[\Delta S_t] = d\) | \(S_t - dt\) is martingale |
These four rows handle the vast majority of interview problems. If none of them work, you’re probably looking at a multi-state system (like HHH or HTH from Puzzle 5b) where recurrences are the right tool.
To check if a process \(M_t\) is already a martingale, compute \(\E[M_{t+1} \mid \mathcal{F}_t]\):
• Equals \(M_t\): it’s a martingale. Done.
• Equals \(M_t + c\): subtract the drift. \(M_t - ct\) is a martingale.
• Equals \(r \cdot M_t\): divide by the growth. \(M_t / r^t\) is a martingale.
The first case is additive (random walks, fair games). The second is a biased additive game. The third is multiplicative (compound returns, branching processes).
\(\E[M_T] = \E[M_0]\) holds if any one of these conditions is met:
(a) \(T\) is bounded a.s.: \(T \leq N\) for some constant \(N\).
(b) Increments bounded (\(|M_{t+1} - M_t| \leq C\)) and \(\E[T] < \infty\).
(c) \(\{M_{t \wedge T}\}\) is uniformly integrable (technical catch-all).
When it fails:
• Doubling strategy: increments unbounded → OST fails → you can “force” \(\E[M_T] \neq \E[M_0]\).
• St. Petersburg: process not integrable → not even a martingale.
In interviews, always state the conditions before applying OST. This is what separates candidates who understand from candidates who memorize.
When given any game, walk through these questions in order:
(1) What is the state? (wealth, position, score, streak length?)
(2) What is the expected one-step change? (compute \(\E[\Delta W_t \mid \text{current state}]\))
(3) Is it zero? (fair game → martingale) If not, what is the drift?
(4) What do I want to find? (hitting probability → try \(f(x) = x\) or \(r^x\); duration → try \(x^2 - t\))
(5) Before using OST: are increments bounded? Is \(\E[T] < \infty\)?
(6) What are the boundary values? (\(S_T \in \{0, n\}\), so \(\E[f(S_T)] = f(n) \cdot w + f(0) \cdot (1-w)\))
Even when you can’t fully solve the problem, walking through this checklist out loud demonstrates exactly the structured thinking interviewers are looking for.
| Problem | Martingale used | Discovery method | OST gives |
|---|---|---|---|
| Ruin probability (fair) | \(S_t\) | Strategy A: \(f(x) = x\) | \(P(\text{win}) = k/n\) |
| Expected duration (fair) | \(S_t^2 - t\) | Strategy B: solve \(\frac{1}{2}g(x\!+\!1) + \frac{1}{2}g(x\!-\!1) = g(x) + 1\) | \(\E[T] = k(n\!-\!k)\) |
| Ruin probability (biased) | \((q/p)^{S_t}\) | Strategy C: solve \(pr^2 - r + q = 0\) | \(P(\text{win}) = \frac{r^k - 1}{r^n - 1}\) |
| Drunkard’s walk (\(n \to \infty\)) | \((q/p)^{S_t}\) + limit | Strategy C + take \(n \to \infty\) | \(P(\text{ruin}) = \begin{cases} 1 & p \leq q \\ (q/p)^k & p > q\end{cases}\) |
The lesson: martingale is the lens; recurrence is the microscope. The martingale approach reveals why an answer has the form it does (because something is conserved). Recurrences grind through the algebra to get the same answer from a different angle. Both are essential — use whichever is faster for the problem at hand.
6. The Broken Stick
A stick of length 1 is broken at two points chosen independently and uniformly at random. What is the probability that the three resulting pieces can form a triangle?
Before reading on: what does it take for three lengths to form a triangle? And is the answer closer to 10%, 25%, 50%, or 75%?
The wrong approach
The temptation is to set up the break points as order statistics \(X_{(1)} < X_{(2)}\), express the three piece lengths, and compute conditional probabilities. This gets tangled in case analysis. A much cleaner approach is to think geometrically.
Map the problem to a geometric region in the plane. The two break points \((U, V)\) are uniformly distributed in the unit square \([0,1]^2\). The triangle inequality conditions carve out a region whose area is the probability.
Let \(U\) and \(V\) be the two break points, uniform on \([0,1]\). Let \(X = \min(U,V)\) and \(Y = \max(U,V)\). The three piece lengths are:
$$ L_1 = X, \qquad L_2 = Y - X, \qquad L_3 = 1 - Y. $$Note: \(L_1 + L_2 + L_3 = 1\).
When can three positive lengths form a triangle? The triangle inequality says no single piece can be longer than the sum of the other two. Since the sum of all three is 1, this simplifies to: no piece can be \(\geq 1/2\).
Crucial observation: at most one piece can be \(\geq 1/2\) (since they sum to 1). So the three “bad” events — \(L_1 \geq 1/2\), \(L_2 \geq 1/2\), \(L_3 \geq 1/2\) — are mutually exclusive. This means:
$$ \Pr[\text{no triangle}] = \Pr[L_1 \geq \tfrac{1}{2}] + \Pr[L_2 \geq \tfrac{1}{2}] + \Pr[L_3 \geq \tfrac{1}{2}]. $$We compute each in the \((U, V)\) unit square.
$$ \Pr[L_1 \geq \tfrac{1}{2}] = \Pr[\min(U,V) \geq \tfrac{1}{2}] = \Pr[U \geq \tfrac{1}{2} \text{ and } V \geq \tfrac{1}{2}] = \left(\tfrac{1}{2}\right)^2 = \tfrac{1}{4}. $$ $$ \Pr[L_3 \geq \tfrac{1}{2}] = \Pr[1 - \max(U,V) \geq \tfrac{1}{2}] = \Pr[\max(U,V) \leq \tfrac{1}{2}] = \left(\tfrac{1}{2}\right)^2 = \tfrac{1}{4}. $$\(\Pr[L_2 \geq 1/2] = \Pr[|U - V| \geq 1/2]\). In the unit square, this is the area of two triangles:
- \(U - V \geq 1/2\): triangle with vertices \((1/2, 0)\), \((1,0)\), \((1, 1/2)\), area \(= 1/8\).
- \(V - U \geq 1/2\): symmetric triangle, area \(= 1/8\).
Total: \(\Pr[L_2 \geq 1/2] = 1/4\).
So \(\Pr[\text{no triangle}] = 1/4 + 1/4 + 1/4 = 3/4\).
When a problem involves continuous random variables (uniform on an interval, region, or volume), the probability equals the area (or volume) of the favorable region divided by the area (or volume) of the sample space.
When to use it: problems involving random points, random lengths, random times, or any continuous uniform distribution. Convert the probability question into a geometric question: “what fraction of the square/cube/region satisfies the condition?”
Other applications: Buffon’s needle, meeting problems (“two friends agree to wait 15 minutes”), random chord problems.
Going Deeper: Geometric Probability at Interview Difficulty
The broken stick is the gateway. In interviews, geometric probability shows up whenever continuous uniform variables meet inequality conditions. The three puzzles below cover the patterns that recur most.
Let \(a, b\) be independent uniform on \([0,1]\). What is the probability that the equation \(x^2 + 2bx + a = 0\) has real roots?
Real roots exist iff the discriminant is non-negative: \((2b)^2 - 4a \geq 0\), i.e., \(b^2 \geq a\).
In the unit square \((b, a) \in [0,1]^2\), this is the region below the parabola \(a = b^2\):
$$ \Pr[\text{real roots}] = \int_0^1 b^2 \, db = \frac{b^3}{3}\bigg|_0^1 = \frac{1}{3}. \quad\square $$The technique: convert the algebraic condition to a geometric region, compute its area. When the boundary is a curve (parabola, circle), integrate; when it’s lines, use triangle/parallelogram formulas.
Two friends agree to meet at a café between noon and 1pm. Each arrives at a uniformly random time and waits 15 minutes. What is the probability they meet?
Let \(X, Y\) be their arrival times, uniform on \([0, 1]\) (in hours). They meet iff \(|X - Y| \leq 1/4\).
In the unit square, the favorable region is the strip between the lines \(Y = X - 1/4\) and \(Y = X + 1/4\). The unfavorable region consists of two corner triangles, each with legs \(3/4\):
$$ \Pr[\text{don't meet}] = 2 \cdot \frac{1}{2}\left(\frac{3}{4}\right)^2 = \frac{9}{16}. $$ $$ \Pr[\text{meet}] = 1 - \frac{9}{16} = \frac{7}{16}. \quad\square $$General formula: with waiting time \(w\) (as a fraction of the window): \(\Pr[\text{meet}] = 1 - (1 - w)^2\).
Break a stick at \(n - 1\) uniformly random points into \(n\) pieces. What is the probability the \(n\) pieces can form a convex \(n\)-gon?
The pieces form an \(n\)-gon iff no single piece exceeds \(1/2\) (no piece \(\geq\) sum of the rest). Let \(A_i\) = event that piece \(i > 1/2\).
Key observation: these events are disjoint — two pieces can’t both exceed half the total. And by symmetry, \(\Pr(A_i)\) is the same for all \(i\).
\(\Pr(A_1) = \Pr(\text{all } n - 1 \text{ cut points fall in the same half}) = (1/2)^{n-1}\).
Since the events are disjoint:
$$ \Pr[\text{some piece} > 1/2] = n \cdot \left(\frac{1}{2}\right)^{n-1} = \frac{n}{2^{n-1}}. $$ $$ \boxed{\Pr[\text{forms } n\text{-gon}] = 1 - \frac{n}{2^{n-1}}.} $$Check \(n = 3\): \(1 - 3/4 = 1/4\). \(\checkmark\) (matches the broken stick triangle answer)
Check \(n = 4\): \(1 - 4/8 = 1/2\). Check \(n = 10\): \(1 - 10/512 \approx 0.98\).
The trick: when bad events are symmetric and disjoint, you skip inclusion-exclusion entirely and just multiply the count by one event’s probability.
The Geometric Probability Playbook
For \(n\) continuous uniform variables, the sample space is the unit \(n\)-cube. The probability IS the volume of the favorable region. Always sketch it before integrating.
If \(X_1, \ldots, X_k\) are i.i.d. continuous (ties have probability 0), then \(\Pr(X_1 < X_2 < \cdots < X_k) = 1/k!\). No integration needed — all \(k!\) orderings are equally likely by symmetry.
When the “bad” events \(A_1, \ldots, A_n\) are symmetric (same probability) and disjoint (at most one can occur): \(\Pr(\text{some } A_i) = n \cdot \Pr(A_1)\). No inclusion-exclusion. This is how the \(n\)-gon problem collapses.
Order Statistics from Scratch
Drop \(n\) points uniformly at random on \([0, 1]\). What’s the expected value of the largest? The smallest? The gap between the two biggest? These questions come up constantly in quant interviews, and they all reduce to two facts about order statistics and spacings.
Given \(n\) i.i.d. random variables \(X_1, \ldots, X_n\), sort them: \(X_{(1)} \leq X_{(2)} \leq \cdots \leq X_{(n)}\). The value \(X_{(k)}\) is the \(k\)-th order statistic. \(X_{(1)}\) = min, \(X_{(n)}\) = max.
If \(X_1, \ldots, X_n\) are i.i.d. Uniform\((0,1)\), then
$$ \E[X_{(k)}] = \frac{k}{n + 1}. $$The \(n\) uniform points divide \([0, 1]\) into \(n + 1\) gaps (spacings): the space before the smallest point, between consecutive points, and after the largest. By symmetry, the \(n + 1\) spacings are exchangeable — any permutation of them is equally likely. Since they sum to 1:
$$ \E[\text{each spacing}] = \frac{1}{n + 1}. $$The \(k\)-th order statistic is the sum of the first \(k\) spacings:
$$ X_{(k)} = \text{(spacing 1)} + \text{(spacing 2)} + \cdots + \text{(spacing } k\text{)}. $$By linearity: \(\E[X_{(k)}] = k \cdot \frac{1}{n+1} = \frac{k}{n+1}\). \(\quad\square\)
Immediate applications:
• \(\E[\text{max of } n \text{ uniforms}] = \E[X_{(n)}] = n/(n+1)\). For \(n = 10\): \(10/11 \approx 0.909\).
• \(\E[\text{min of } n \text{ uniforms}] = \E[X_{(1)}] = 1/(n+1)\). For \(n = 10\): \(1/11 \approx 0.091\).
• \(\E[\text{median}] \approx 1/2\) (exactly \((n+1)/(2(n+1)) = 1/2\) when \(n\) is odd).
The \(n + 1\) spacings created by \(n\) uniform points on \([0,1]\) are exchangeable. Every spacing has the same expected value \(1/(n+1)\), and the expected value of the gap between any two consecutive order statistics is \(1/(n+1)\).
Drop \(n\) uniform points on \([0,1]\). What is \(\E[X_{(n)} - X_{(n-1)}]\)?
\(X_{(n)} - X_{(n-1)}\) is one of the \(n + 1\) spacings. By exchangeability, each spacing has the same expected value:
$$ \E[X_{(n)} - X_{(n-1)}] = \frac{1}{n + 1}. \quad\square $$The surprise: the expected gap between the two largest points equals the expected gap between the two smallest, or between any other consecutive pair. The specific identity of “which gap” is irrelevant — exchangeability makes them all equal.
Drop \(n\) points uniformly on a circle of circumference 1. Pick a fixed point \(P\) on the circle. What is the expected length of the arc containing \(P\)?
The \(n\) points cut the circle into \(n\) arcs. Each arc has expected length \(1/n\). So the arc containing \(P\) has expected length \(1/n\), right?
Wrong. The arc containing \(P\) is not a uniformly chosen arc. Longer arcs are more likely to contain \(P\) — this is size-biased sampling.
Here is the correct calculation. Think of the circle as \([0, 1)\) with endpoints identified, and place \(P\) at 0. The arc containing \(P\) is the gap between the largest point (going counterclockwise) and the smallest point (going clockwise) — equivalently, the gap containing 0 when \(n\) points are dropped on \([0, 1)\).
This gap is the sum of the first and last spacing on the line \([0, 1]\) when we “unwrap” the circle. But there is a cleaner argument: by symmetry, the \(n\) random points and the fixed point \(P\) form \(n + 1\) points, creating \(n + 1\) arcs on the circle, each with expected length \(1/(n+1)\). The arc containing \(P\) spans from the point just before \(P\) to the point just after, covering two of these \(n + 1\) spacings.
$$ \E[\text{arc containing } P] = \frac{2}{n + 1}. \quad\square $$Check \(n = 1\): one point splits the circle into one arc of length 1. The arc containing \(P\) has length 1. Formula: \(2/2 = 1\). \(\checkmark\)
When you sample an interval by picking a random point inside it, you get the length-weighted distribution, not the uniform one. Longer intervals are more likely to be “hit.” This is the same reason the bus you catch is more crowded than the average bus, your friends have more friends than you do (the friendship paradox), and the class you attend has more students than the average class. Whenever you see “containing a random point,” expect the answer to be larger than the naive average.
The Order Statistics Playbook
\(\E[k\text{-th of } n \text{ uniforms}] = k/(n+1)\). The \(n + 1\) in the denominator accounts for the \(n + 1\) spacings. This single formula answers every “expected max,” “expected min,” and “expected median” question on the spot.
\(\E[\text{any specific spacing}] = 1/(n+1)\). The expected gap between the 3rd and 4th largest equals the expected gap between the 99th and 100th largest. Don’t waste time identifying “which gap” — they’re all the same in expectation.
“The interval containing a random point” \(\neq\) “a random interval.” The former is length-weighted: \(\E = 2/(n+1)\), not \(1/n\). Whenever the problem says “containing,” “covering,” or “the one you observe,” check for size-biasing.
Generating Functions from Scratch
Recurrences are the bread and butter of quant puzzles. Generating functions are the machine that solves them: encode a sequence as a power series, and the recurrence becomes algebra. They also give you the full distribution of a random variable, not just its mean.
Flip a fair coin \(n\) times. What is the probability of never seeing two consecutive heads?
Let \(f(n)\) = number of length-\(n\) binary strings with no “HH.” A valid string either:
• ends in T: the first \(n - 1\) characters form any valid string of length \(n - 1\), or
• ends in TH: the first \(n - 2\) characters form any valid string of length \(n - 2\).
(It can’t end in HH — that’s forbidden.)
This gives the recurrence:
$$ f(n) = f(n - 1) + f(n - 2). $$This is Fibonacci! With initial conditions \(f(1) = 2\) (H or T) and \(f(2) = 3\) (HT, TH, TT):
$$ f(n) = F_{n+2} \quad \text{(the } (n+2)\text{-th Fibonacci number)}. $$The probability is:
$$ \Pr[\text{no HH in } n \text{ flips}] = \frac{F_{n+2}}{2^n}. $$Growth rate: \(F_{n+2} \sim \varphi^{n+2}/\sqrt{5}\) where \(\varphi = (1 + \sqrt{5})/2 \approx 1.618\), so the probability decays like \((\varphi/2)^n \approx (0.809)^n\).
For \(n = 10\): \(F_{12} = 144\), so \(\Pr = 144/1024 \approx 14.1\%\). \(\quad\square\)
“No two consecutive X” → Fibonacci-type recurrence. This shows up constantly: no two adjacent occupied seats, no two consecutive 1s in a binary code, tiling a \(2 \times n\) board with dominoes (also \(F_{n+1}\)). Once you spot the “what can the last element be?” structure, the recurrence writes itself.
Encode a sequence \(a_0, a_1, a_2, \ldots\) as a power series:
$$ A(x) = \sum_{n=0}^{\infty} a_n x^n. $$A linear recurrence with constant coefficients → \(A(x)\) is a rational function (ratio of polynomials). Partial fractions → closed form for \(a_n\).
Example: Fibonacci. \(a_n = a_{n-1} + a_{n-2}\) becomes \(A(x) = (a_0 + (a_1 - a_0)x)/(1 - x - x^2)\). Factoring the denominator and using partial fractions gives Binet’s formula: \(F_n = (\varphi^n - \psi^n)/\sqrt{5}\) where \(\psi = (1 - \sqrt{5})/2\).
For a non-negative integer-valued random variable \(X\):
$$ G_X(s) = \E[s^X] = \sum_{k=0}^{\infty} \Pr(X = k) \, s^k. $$Key properties:
• \(G_X(1) = 1\) (probabilities sum to 1).
• \(G_X'(1) = \E[X]\).
• \(G_X''(1) = \E[X(X-1)]\), so \(\Var(X) = G''(1) + G'(1) - [G'(1)]^2\).
• If \(X, Y\) are independent: \(G_{X+Y}(s) = G_X(s) \cdot G_Y(s)\).
• Compound sums: if \(S = X_1 + \cdots + X_N\) where \(N\) is random and the \(X_i\) are i.i.d., independent of \(N\): \(G_S(s) = G_N(G_X(s))\).
Let \(S = X_1 + X_2 + \cdots + X_N\) where \(N\) is a random variable, the \(X_i\) are i.i.d., and \(N\) is independent of the \(X_i\)’s. What is \(\E[S]\)?
Condition on \(N\) (the tower property):
$$ \E[S] = \E[\E[S \mid N]] = \E[N \cdot \E[X]] = \E[N] \cdot \E[X]. $$This is Wald’s identity: the expected sum of a random number of random variables equals the expected count times the expected individual value.
Example: a casino game lasts \(N \sim \text{Geometric}(1/4)\) rounds (expected 4 rounds). Each round you win \(X_i\) with \(\E[X_i] = 2\). Expected total winnings: \(\E[S] = 4 \cdot 2 = 8\). \(\quad\square\)
The Generating Functions Playbook
Whenever you see “no two adjacent/consecutive X,” write the recurrence \(f(n) = f(n-1) + f(n-2)\) (Fibonacci). If the constraint is “no three consecutive,” it becomes \(f(n) = f(n-1) + f(n-2) + f(n-3)\) (Tribonacci). The characteristic equation and GF machinery handle any such recurrence mechanically.
For “sum of a random number of random variables”:
• Mean: \(\E[S] = \E[N] \cdot \E[X]\) (Wald).
• Variance: \(\Var(S) = \E[N]\Var(X) + \Var(N)[\E[X]]^2\).
• Full distribution: \(G_S(s) = G_N(G_X(s))\).
Use recurrences (first-step analysis) when you only need the mean. Use generating functions when you need the full distribution, a closed form for a recurrence, or when you have a compound sum. GFs are overkill for most interview puzzles — but when the problem asks for the variance or the probability of a specific outcome, they’re the right tool.
Conditional Expectation from Scratch
“Condition on the thing you wish you knew.” This one sentence summarizes the most versatile technique in probability. We already used it in first-step analysis (condition on the first move). Here we formalize it and push it to the problems where it truly shines: mixtures, hidden parameters, and Gaussian conditioning.
For any random variables \(X\) and \(Y\):
$$ \E[\E[X \mid Y]] = \E[X]. $$In words: compute the conditional expectation of \(X\) given \(Y\), then average over all values of \(Y\) — you get back the unconditional expectation of \(X\). This is “the law of total expectation.”
Why it’s powerful: when \(\E[X]\) is hard to compute directly, find a variable \(Y\) such that \(\E[X \mid Y]\) is tractable, then average over \(Y\). Every first-step analysis argument (\(\E[T] = \sum_y \E[T \mid \text{first step} = y] \cdot \Pr(\text{first step} = y)\)) is the tower property in action.
Pick \(p\) uniformly at random from \([0, 1]\). Then flip a coin with heads probability \(p\) until the first heads. What is the expected number of flips?
Condition on \(p\). Given \(p\), the number of flips is Geometric\((p)\) with mean \(1/p\). By the tower property:
$$ \E[\text{flips}] = \E\!\left[\E[\text{flips} \mid p]\right] = \E\!\left[\frac{1}{p}\right] = \int_0^1 \frac{1}{p} \, dp = \ln p \Big|_0^1. $$This integral diverges. The expected number of flips is infinite.
Why: for any fixed \(p > 0\), you’d finish in finite expected time (\(1/p\) flips). But values of \(p\) near 0 — almost-all-tails coins — contribute \(\E[\text{flips} \mid p] \approx 1/p \to \infty\). Even though these coins are “rare” (small \(p\)), their contribution to the expectation is not integrable. The fat tail wins.
This is a “trust the math over intuition” answer. In interviews, it tests whether you can set up the tower property cleanly and accept a divergent answer when the math says so. \(\quad\square\)
Let \(X, Y\) be standard normal random variables with correlation \(\rho\). What is \(\E[Y \mid X = x]\)?
The key decomposition: write \(Y\) as the part explained by \(X\) plus independent noise:
$$ Y = \rho X + \sqrt{1 - \rho^2} \, Z, \qquad Z \sim N(0,1), \quad Z \perp X. $$Check: \(\E[Y] = 0\), \(\Var(Y) = \rho^2 + (1 - \rho^2) = 1\), \(\text{Corr}(X, Y) = \rho\). \(\checkmark\)
Now condition on \(X = x\). Since \(Z\) is independent of \(X\):
$$ \E[Y \mid X = x] = \rho x + \sqrt{1 - \rho^2} \cdot \E[Z] = \rho x + 0 = \rho x. $$ $$ \boxed{\E[Y \mid X = x] = \rho x.} $$The conditional expectation of \(Y\) given \(X\) is the regression line through the origin with slope \(\rho\). This is the probabilistic foundation of linear regression: the best linear prediction of \(Y\) from \(X\) is exactly \(\rho X\).
Conditional variance: \(\Var(Y \mid X = x) = (1 - \rho^2) \cdot \Var(Z) = 1 - \rho^2\). This is the “unexplained” variance — the fraction of \(Y\)’s variability not captured by \(X\). When \(\rho = \pm 1\), the conditional variance is 0 (perfect prediction). When \(\rho = 0\), it equals 1 (knowing \(X\) tells you nothing about \(Y\)). \(\quad\square\)
The Conditional Expectation Playbook
When you can’t compute \(\E[X]\) directly, find a variable \(Y\) such that \(\E[X \mid Y = y]\) has a closed form. Then integrate: \(\E[X] = \int \E[X \mid Y = y] \, f_Y(y) \, dy\). If this integral diverges, the answer is infinity — and that is the answer.
For standardized bivariate normals with correlation \(\rho\):
• \(\E[Y \mid X = x] = \rho x\) (the regression line).
• \(\Var(Y \mid X = x) = 1 - \rho^2\) (the unexplained variance).
• The conditional distribution \(Y \mid X = x\) is \(N(\rho x, 1 - \rho^2)\).
This is the fact behind every linear-model question in a quant interview. If you remember one formula from this section, remember \(\E[Y \mid X = x] = \rho x\).
When a problem says “first draw a random parameter \(\theta\), then...” it’s asking you to use the tower property. Compute \(\E[X \mid \theta]\), then average over \(\theta\). Watch for divergence: if \(\E[X \mid \theta]\) diverges for any positive-measure set of \(\theta\) values, \(\E[X]\) may be infinite (as in the random coin problem).
7. The Ballot Problem
In an election, candidate A receives \(a\) votes and candidate B receives \(b\) votes, with \(a > b\). The ballots are counted in a uniformly random order. What is the probability that A is strictly ahead of B throughout the entire counting process (after every ballot counted)?
Before reading on: try the smallest non-trivial case. If \(a = 2, b = 1\), there are \(\binom{3}{2} = 3\) equally likely orderings: AAB, ABA, BAA. In which of these is A strictly ahead throughout?
- AAB: Running tally: A leads 1–0, 2–0, 2–1. Always ahead. \(\checkmark\)
- ABA: Running tally: A leads 1–0, 1–1 (tied!), 2–1. Not strictly ahead throughout. \(\times\)
- BAA: Running tally: B leads 0–1. Not ahead. \(\times\)
Answer: \(1/3\). And indeed \((a - b)/(a + b) = (2 - 1)/(2 + 1) = 1/3\). The pattern holds.
Model the counting process as a lattice path: each A-vote is a step \(+1\), each B-vote is a step \(-1\). The path goes from \(0\) to \(a - b\) in \(n = a + b\) steps. “A is strictly ahead throughout” means the path stays strictly positive for all steps \(1, 2, \ldots, n\). The reflection principle counts paths that touch zero by reflecting them into paths that end at a different height.
Let \(n = a + b\). A counting sequence is a permutation of \(a\) copies of \(+1\) and \(b\) copies of \(-1\). There are \(\binom{n}{a}\) such sequences, each equally likely.
Step 1: The first vote must be for A (otherwise B is ahead immediately). This happens with probability \(a/n\).
Step 2: Given the first vote is for A, we have a path starting at height 1 with \(a-1\) remaining \(+1\) steps and \(b\) remaining \(-1\) steps, ending at height \(a - b\). We need this path to stay strictly above 0. Let’s count these “good” paths using the reflection principle.
Total paths from height 1 to height \(a - b\) in \(n - 1\) steps: \(\binom{n-1}{a-1}\).
“Bad” paths: those that touch height 0 at some point. Reflect the portion of the path before the first touch of 0 around the horizontal axis. This creates a bijection between bad paths (from height 1 to height \(a-b\), touching 0) and all paths from height \(-1\) to height \(a-b\) in \(n-1\) steps.
A path from \(-1\) to \(a - b\) in \(n - 1\) steps needs total displacement \(a - b + 1\), so it has \(a\) up-steps and \(b - 1\) down-steps. Number of such paths: \(\binom{n-1}{a}\).
Step 3: Good paths from height 1 = total minus bad:
$$ \binom{n-1}{a-1} - \binom{n-1}{a}. $$Using \(\binom{n-1}{a} = \binom{n-1}{a-1} \cdot \frac{n - a}{a} = \binom{n-1}{a-1} \cdot \frac{b}{a}\):
$$ \binom{n-1}{a-1} - \binom{n-1}{a} = \binom{n-1}{a-1}\left(1 - \frac{b}{a}\right) = \binom{n-1}{a-1} \cdot \frac{a - b}{a}. $$Step 4: The probability that A is always ahead, given the first vote is A:
$$ \frac{\binom{n-1}{a-1} \cdot \frac{a-b}{a}}{\binom{n-1}{a-1}} = \frac{a - b}{a}. $$Step 5: Combining with the probability the first vote is A:
$$ \Pr[\text{A always ahead}] = \frac{a}{n} \cdot \frac{a - b}{a} = \frac{a - b}{n} = \frac{a - b}{a + b}. \quad \square $$For \(a = 3, b = 1\) (4 ballots, 4 orderings of AAAB):
| Order | Running lead | Always ahead? |
|---|---|---|
| AAAB | 1, 2, 3, 2 | \(\checkmark\) |
| AABA | 1, 2, 1, 2 | \(\checkmark\) |
| ABAA | 1, 0, 1, 2 | \(\times\) (tied) |
| BAAA | −1, 0, 1, 2 | \(\times\) |
Good orderings: 2 out of 4. Formula: \((3 - 1)/(3 + 1) = 2/4 = 1/2\). \(\checkmark\)
To count lattice paths that avoid a boundary, count all paths and subtract the “bad” ones. Bad paths (those that touch the boundary) are in bijection with paths ending at a reflected endpoint, via reflecting the prefix before the first boundary touch.
When to use it: random walk problems with barriers, first-passage times, maximum of a random walk, Catalan numbers, Dyck paths. Any time you need to count paths that stay in a region.
8. The Secretary Problem
You need to hire one secretary out of \(n\) applicants. You interview them one by one, in a uniformly random order. After each interview, you can see how the current candidate ranks relative to all candidates you have seen so far, and you must immediately accept or reject them — no callbacks. If you reject everyone, you are stuck with the last candidate.
What strategy maximizes the probability of hiring the single best candidate?
Before reading on: think about what makes this problem hard. You can’t compare candidates to ones you haven’t seen yet. If you accept too early, you might miss the best. If you wait too long, the best might have already passed.
The wrong approaches
Accept the first candidate: probability of getting the best = \(1/n\). Terrible for large \(n\).
Wait for a candidate who “seems good enough”: what threshold do you use? You have no absolute scale — only relative rankings among those you’ve seen. “Best so far” doesn’t mean “best overall.”
Explore, then exploit. Use the first \(r\) candidates as a “learning phase” — reject all of them, but remember the best you saw. Then, in the “exploit phase,” accept the first candidate who is better than everyone in the learning phase. The optimal \(r\) turns out to be \(n/e\), and the resulting success probability approaches \(1/e \approx 36.8\%\).
The strategy: fix a threshold \(r\) (we will optimize it later). Reject the first \(r\) candidates. Then accept the first subsequent candidate who is the best seen so far (i.e., better than all \(r\) candidates in the learning phase). If no such candidate appears, accept the last one.
Computing \(\Pr[\text{win}]\). The best candidate is in position \(k\) (for some \(k = 1, \ldots, n\), each equally likely). We select the best candidate if and only if:
- The best is not in the learning phase: \(k > r\).
- We don’t stop before reaching position \(k\): the best of the first \(k - 1\) candidates must be among the first \(r\) (otherwise someone in positions \(r+1\) through \(k-1\) would have been selected first).
\(\Pr[\text{best of first } k-1 \text{ is in first } r] = r/(k-1)\) (each of the \(k-1\) candidates is equally likely to be the best among them).
$$ \begin{aligned} \Pr[\text{win} \mid \text{strategy } r] &= \sum_{k=r+1}^{n} \Pr[\text{best at position } k] \cdot \Pr[\text{don't stop early} \mid k] \\ &= \sum_{k=r+1}^{n} \frac{1}{n} \cdot \frac{r}{k-1}. \end{aligned} $$ $$ = \frac{r}{n} \sum_{k=r+1}^{n} \frac{1}{k-1} = \frac{r}{n} \sum_{j=r}^{n-1} \frac{1}{j}. $$Optimizing \(r\) for large \(n\). For large \(n\), the sum is approximately an integral:
$$ \frac{r}{n} \sum_{j=r}^{n-1} \frac{1}{j} \approx \frac{r}{n} \int_r^n \frac{dx}{x} = \frac{r}{n} \ln\frac{n}{r}. $$Let \(t = r/n\) (the fraction of candidates in the learning phase). Then:
$$ \Pr[\text{win}] \approx -t \ln t. $$Maximize: \(\frac{d}{dt}[-t\ln t] = -\ln t - 1 = 0\), giving \(t^* = 1/e\). The optimal success probability is:
$$ \Pr[\text{win}] = \frac{1}{e} \cdot \ln(e) = \frac{1}{e} \approx 0.368. $$This is remarkable. Even though you see candidates in a random order, can never go back, and have no absolute scale — you can find the single best candidate more than a third of the time. And no strategy can do better: \(1/e\) is the theoretical optimum.
With \(n = 4\), the optimal threshold is \(r = 1\) (reject 1, then accept the next candidate who is the best so far). There are \(4! = 24\) orderings. Let’s trace through a few where the best candidate is in position 3 (labeled as the number 4):
- \((2, 1, 4, 3)\): reject 2. See 1 (not better than 2). See 4 (better than 2!). Accept. Win! \(\checkmark\)
- \((1, 3, 4, 2)\): reject 1. See 3 (better than 1!). Accept 3. But 3 is not the best — 4 is. Lose. \(\times\)
Across all 24 orderings with \(r = 1\): we win 11 times = 11/24 \(\approx\) 45.8%. The large-\(n\) limit of 36.8% is approached from above.
When you must make an irrevocable decision based on sequentially revealed information, the optimal strategy often has a two-phase structure: gather information first (explore), then act on the first opportunity that exceeds your learned threshold (exploit).
When to use it: hiring problems, house-buying problems, any sequential decision-making under uncertainty where you can’t revisit past options.
The \(1/e\) law: in the basic secretary problem, the optimal explore phase uses the first \(1/e \approx 37\%\) of opportunities. This constant appears universally in optimal stopping theory.
You may roll a fair die up to 3 times. After each roll you can stop and take the current face value as your payout (in dollars), or continue rolling. What is the optimal strategy, and what is the expected payout?
Solve from the last decision and work backward — this is the universal method for finite-horizon stopping problems.
Round 3 (forced): you must keep whatever you roll. \(\E[\text{payout}] = 3.5\).
Round 2: you see a face \(x\). Keep if \(x > 3.5\) (better than the expected value of being forced in round 3), re-roll if \(x \leq 3.5\). So keep on 4, 5, 6; re-roll on 1, 2, 3.
$$ \E[\text{round 2}] = \frac{1}{2} \cdot \frac{4 + 5 + 6}{3} + \frac{1}{2} \cdot 3.5 = \frac{1}{2}(5) + \frac{1}{2}(3.5) = 4.25. $$Round 1: keep if \(x > 4.25\), i.e., keep on 5 or 6; re-roll on 1, 2, 3, 4.
$$ \E[\text{round 1}] = \frac{2}{6} \cdot \frac{5 + 6}{2} + \frac{4}{6} \cdot 4.25 = \frac{1}{3}(5.5) + \frac{2}{3}(4.25) = \frac{14}{3} \approx 4.667. $$Optimal policy: round 1 keep 5–6, round 2 keep 4–6, round 3 forced. Expected payout: \(14/3\).
The principle: at each round, the threshold = expected value of continuing. The thresholds decrease as you approach the deadline (you become less picky as time runs out). This is backward induction — the same logic behind the secretary problem, taken to its most explicit form.
9. The 100 Prisoners Problem
100 prisoners, numbered 1 through 100, are given a challenge. In a room, there are 100 drawers, also numbered 1 through 100. Inside the drawers, a random permutation of the numbers 1 through 100 has been placed — one number per drawer.
Each prisoner enters the room one at a time, opens at most 50 drawers, and must find the drawer containing their own number. The prisoners may strategize beforehand, but once the process begins, they cannot communicate (no leaving signals, no rearranging drawers). If every prisoner finds their number, they all go free. If even one fails, they all stay imprisoned.
What strategy maximizes their probability of all going free?
Before reading on: with random guessing, each prisoner finds their number with probability \(50/100 = 1/2\). The probability that all 100 succeed independently is \((1/2)^{100} \approx 8 \times 10^{-31}\). That is effectively zero. Can they possibly do better?
The naive approach: random guessing
Each prisoner opens 50 drawers at random. \(\Pr[\text{all succeed}] = (1/2)^{100} \approx 10^{-31}\). A number so small that if every person on Earth tried this challenge a billion times per second for the age of the universe, the probability of even one success would be negligible.
Can a strategy possibly overcome this? The prisoners can’t communicate during the process, so each prisoner makes their decisions independently. How could coordination help?
The drawers contain a permutation. Permutations have cycle structure. If prisoner \(k\) starts at drawer \(k\) and follows the chain — opens drawer \(k\), finds number \(j\), opens drawer \(j\), finds number \(m\), opens drawer \(m\), and so on — they will eventually loop back to finding number \(k\), completing a cycle. They succeed if and only if their cycle has length \(\leq 50\).
The strategy creates a global coupling: all prisoners succeed or fail together based on the cycle structure of a single permutation. This replaces 100 independent coin flips with one shared random object.
Permutation cycles
Before solving the problem, let’s understand what a cycle in a permutation is. Consider a small example: the permutation \(\pi = (3, 1, 4, 2)\) on \(\{1, 2, 3, 4\}\), meaning drawer 1 contains 3, drawer 2 contains 1, drawer 3 contains 4, drawer 4 contains 2.
Follow the chains:
- Start at 1: \(1 \to 3 \to 4 \to 2 \to 1\). This is a single cycle of length 4: \((1\ 3\ 4\ 2)\).
Every element appears in exactly one cycle (because \(\pi\) is a bijection). A random permutation of \(\{1, \ldots, n\}\) decomposes into disjoint cycles, and the distribution of cycle lengths is well-understood.
To form a cycle of length \(\ell\):
- Choose \(\ell\) elements from \(n\): \(\binom{n}{\ell}\) ways.
- Arrange them in a cycle: \((\ell - 1)!\) ways (fix one element, arrange the remaining \(\ell - 1\) in a line).
- The remaining \(n - \ell\) elements can form any permutation: \((n - \ell)!\) ways.
Number of permutations with a cycle of length \(\ell\) containing specific elements is \((\ell-1)! \cdot (n-\ell)!\). Total permutations with any such cycle:
$$ \begin{aligned} \binom{n}{\ell} \cdot (\ell-1)! \cdot (n-\ell)! &= \frac{n!}{\ell!(n-\ell)!} \cdot (\ell-1)! \cdot (n-\ell)! \\ &= \frac{n!}{\ell}. \end{aligned} $$Dividing by the total \(n!\) permutations:
$$ \Pr[\text{cycle of length } \ell] = \frac{n!/\ell}{n!} = \frac{1}{\ell}. \quad \square $$This is a remarkable result: the probability doesn’t depend on \(n\). A random permutation of 100 elements has a cycle of length 73 with probability \(1/73\), just as a permutation of a million elements does.
The strategy: prisoner \(k\) starts at drawer \(k\) and follows the chain. They succeed iff their cycle has length \(\leq 50\).
All prisoners succeed iff every cycle has length \(\leq 50\), i.e., the longest cycle has length \(\leq 50\).
Key observation: a random permutation of \(\{1, \ldots, 100\}\) can have at most one cycle of length \(> 50\) (because such a cycle uses more than half the elements, leaving no room for another long cycle). So:
$$ \Pr[\text{longest cycle} > 50] = \Pr\!\left[\bigcup_{\ell=51}^{100} \{\text{cycle of length } \ell \text{ exists}\}\right]. $$Since these events are mutually exclusive (at most one long cycle):
$$ = \sum_{\ell=51}^{100} \Pr[\text{cycle of length } \ell] = \sum_{\ell=51}^{100} \frac{1}{\ell} = \frac{1}{51} + \frac{1}{52} + \cdots + \frac{1}{100}. $$This is a partial sum of the harmonic series. Using the integral approximation:
$$ \sum_{\ell=51}^{100} \frac{1}{\ell} \approx \int_{50}^{100} \frac{dx}{x} = \ln\frac{100}{50} = \ln 2 \approx 0.6931. $$So:
$$ \Pr[\text{all succeed}] = 1 - \sum_{\ell=51}^{100} \frac{1}{\ell} \approx 1 - \ln 2 \approx 0.3069. $$Absorb this for a moment. Random guessing gives a probability of \((1/2)^{100} \approx 10^{-31}\). The cycle strategy gives \(31.2\%\). That is an improvement by a factor of approximately \(10^{30}\). Thirty orders of magnitude. This is not a marginal improvement — it is the difference between “impossible in the lifetime of the universe” and “works about one time in three.”
And this is provably optimal: no strategy can exceed \(1 - \ln 2\). The bottleneck is entirely the probability that the random permutation contains a cycle longer than \(n/2\), and no strategy can change the permutation.
The magic of the cycle strategy is that it creates perfect correlation between the prisoners’ outcomes. With random guessing, the 100 prisoners face 100 independent events, and the probability of all succeeding is \((1/2)^{100}\). With the cycle strategy, all 100 prisoners succeed or fail based on a single global property of the permutation: whether its longest cycle is \(\leq 50\).
The lesson: when you can’t change the probability that each individual succeeds (still \(1/2\) per prisoner), correlating their outcomes is the key to collective success.
When a problem involves a bijection (a one-to-one mapping), decompose it into cycles. Random permutations have well-understood cycle statistics: P(cycle of length \(\ell\)) = \(1/\ell\), and the longest cycle has length \(\sim n/e^\gamma\) on average.
When to use it: problems involving random assignments, random matchings, card shuffling, or any situation where objects are mapped to objects via a bijection. The cycle structure often reveals hidden regularity in what looks like chaos.
The Problem-Solver’s Toolkit
Nine puzzles, multiple deep dives, and several standalone technique sections. Here is the complete toolkit, side by side.
| # | Technique | Core Move | Trigger Phrase |
|---|---|---|---|
| 1 | Reframing | Find an equivalent problem that’s trivial | “Interacting particles,” “identical agents” |
| 2 | Complement Counting + Bayes | Compute P(none), or condition on HOW info arrived | “At least one,” “given that,” “I tell you” |
| 3 | Indicators + Linearity | Write the count as ΣXi and add marginals | “Expected number of...” |
| 4 | Geometric Decomposition | Break into phases, each with a geometric wait | “How long until...,” “collect all” |
| 5 | First-Step Analysis | Condition on the first move → recurrence | “Random walk,” “absorbing barrier” |
| 6 | Martingales + OST | Find conserved quantity, apply \(\E[M_T] = \E[M_0]\) | “Fair game,” “is it fair?,” “pattern waiting time” |
| 7 | Geometric Probability | Map to a region, compute area/volume | “Uniformly random on [0,1],” continuous inequalities |
| 8 | Order Statistics + Spacings | k-th of n uniforms: mean \(k/(n+1)\) | “Expected max/min,” “expected gap,” “random points” |
| 9 | Generating Functions | Encode sequence as power series, recurrence → algebra | “No two consecutive,” “sum of random number of” |
| 10 | Conditional Expectation | Tower: \(\E[\E[X \mid Y]] = \E[X]\) | “First draw a parameter, then...,” “given partial info” |
| 11 | Reflection Principle | Count bad paths via bijection with reflected paths | “Stay above/below,” “never touch” |
| 12 | Optimal Stopping | Backward induction: threshold = continuation value | “Sequential,” “irrevocable,” “when should I stop?” |
| 13 | Permutation Cycles | Decompose a bijection into cycles | “Random assignment,” “random matching” |
The Recognition Flowchart
The skill the firms are testing isn’t recall — it’s how fast you classify a problem into the right bucket. Here is the decision tree, in priority order:
(1) “Expected number of...” → Indicators + linearity. Don’t even think about joint distributions.
(2) “Expected time/steps until...” → First-step recursion (state → recurrence) or a martingale (find conserved quantity). For patterns (HHH, HTH, ABRACADABRA), think overlap structure.
(3) Continuous uniforms with inequalities? → Count orderings if symmetric (\(1/k!\)); else integrate over the unit square/cube. For max/min/gaps, use order statistics (\(k/(n+1)\)).
(4) “Given that...” / observed information? → Stop and define the generating process. “At least one is a boy” ≠ “I picked one and it’s a boy.” The procedure matters.
(5) “When should I stop?” → Backward induction from the last decision. Threshold = continuation value.
(6) Anything Gaussian / “expected value given”? → Conditional expectation. \(\E[Y \mid X = x] = \rho x\) for standardized bivariate normals.
(7) Numbers too clean / looks impossible? → Search for an invariant, coupling, or symmetry before computing anything.
| Fact | Value |
|---|---|
| Records/cycles in random permutation of \(n\) | \(H_n \approx \ln n + \gamma\) |
| Fixed points in random permutation | Exactly 1 (any \(n\)) |
| \(\E[\text{HH}]\) waiting, fair coin | 6 |
| \(\E[\text{HT}]\) waiting, fair coin | 4 |
| \(\E[\text{ABRACADABRA}]\) on 26-letter alphabet | \(26^{11} + 26^4 + 26^1\) |
| Gambler’s ruin: \(P(\text{reach } N \text{ from } i)\), fair | \(i/N\) |
| Gambler’s ruin: expected duration, fair | \(i(N - i)\) |
| k-th order stat of \(n\) uniforms: mean | \(k/(n+1)\) |
| Spacings: expected gap | \(1/(n+1)\) |
| Arc containing a fixed point (\(n\) random pts on circle) | \(2/(n+1)\) |
| No two consecutive heads in \(n\) flips | \(F_{n+2}/2^n\) |
| Secretary problem: reject first... | \(n/e\); win prob \(1/e \approx 0.368\) |
| Bivariate normal: \(\E[Y \mid X = x]\) | \(\rho x\) |
| 100 prisoners: cycle-following strategy | \(1 - \ln 2 \approx 0.311\) |
| “At least one boy” → P(both boys) | \(1/3\) |
| “Random child is a boy” → P(both boys) | \(1/2\) |
| Tuesday Boy → P(both boys) | \(13/27 \approx 0.481\) |
| Random quadratic \(x^2 + 2bx + a = 0\) real roots | \(1/3\) |
The meta-lesson is this: quant interviews are not testing whether you have memorized the answer to the birthday problem. They are testing whether you can recognize that a problem you have never seen before is secretly a birthday problem — or a coupon collector, or a random walk with absorbing barriers, or a geometric probability question. The answer is the easy part. Finding the right angle of attack is everything.
If you worked through every puzzle with pen and paper, you now have a full arsenal of reflexes that weren’t there before. When someone describes a problem with interacting identical particles, you feel the pull toward reframing. When you hear “expected number of,” your hand reaches for indicator variables. When a process has a Markov property, you instinctively condition on the first step. When someone asks “is this fair?,” you write the wealth process and check the drift.
These reflexes don’t come from reading. They come from doing. That is why the pen and the notebook matter.