First-step analysis: look one move ahead, then solve
“How many coin flips, on average, until you see two heads in a row?” You can’t just count outcomes: the game has no fixed length. First-step analysis handles this by giving every situation an unknown, looking one flip ahead, and turning the question into a few lines of algebra.
Expected rolls until a 6
Let e be the expected number of rolls. Roll once. With probability 1/6 you are done. With probability 5/6 you got nothing, and you are back exactly where you started, so you still expect e more rolls.
The trick is that the unknown appears on both sides. The process restarts, so the future after a bad roll looks exactly like the future from the start. That self-similarity is what first-step analysis exploits, and it is why the method needs no infinite sums.
HH takes 6 flips on average, HT only 4
For “HH”, the states are start (no useful progress) and H (last flip was a head). Write one equation per state by asking where the next flip sends you:
Substitute the second into the first: , so and .
Now “HT”. From state H, a T finishes and another H keeps you in H: you never lose your progress. So gives , and . Same probability per attempt, different answer, because of what happens when you fail. Pick a pattern below and watch the fall-back arrows.
First-step equations
Solution
Hover a state in the diagram to see its expected remaining flips.
The grey arrows are where the method earns its keep. In “HTH”, a T from state HT sends you all the way back to start, but in “HHT” an extra H from state HH keeps you at HH. The rule is always the same: after a miss, move to the longest beginning of the pattern that still matches the end of what you have seen.
Shortcut for fair coins: the overlap rule
For a pattern of length n, add for every k where the first k letters equal the last k letters (k = n always counts). HH: 4 + 2 = 6. HT: 4. HHH: 8 + 4 + 2 = 14. HTH: 8 + 2 = 10. HTHT: 16 + 4 = 20. Good for checking an answer fast, but in an interview, show the first-step equations.
Gambler’s ruin: who hits their boundary first?
You have i dollars and bet 1 dollar a round, winning with probability p (losing with q = 1 − p). You stop at 0 (ruin) or N (target). Let be the chance you reach N first. Conditioning on the first round:
Rearranging gives : the steps between neighbours form a geometric sequence. Summing them and using :
The expected length of the game uses the same equation with a +1, with . For a fair game the answer is .
P(reach N before 0), by start
Expected rounds until the game ends
Move p a little below 0.5 and watch the left chart sag. A small edge compounds over a long game: with p = 0.49, starting at 50 and aiming for 100, you win only 11.9% of the time, not 50%. At American roulette (p = 18/38 on red) the same plan wins 0.5% of the time.
Many states: write it as a matrix
With more than three or four states, solving by hand gets slow. Collect the non-final states into a matrix Q of transition probabilities between them. The first-step equations are then one line, and so is the solution:
import numpy as np
# Waiting for HH. States: 0 = start, 1 = "last flip was H". The finished state is left out.
Q = np.array([[0.5, 0.5], # start: T -> start, H -> "H"
[0.5, 0.0]]) # "H": T -> start, H -> done (not in Q)
e = np.linalg.solve(np.eye(2) - Q, np.ones(2))
print(e) # [6. 4.]is called the fundamental matrix. Its (s, t) entry is the expected number of visits to t starting from s. Absorption probabilities come from the same matrix: multiply it by R, the transition probabilities from non-final states into the final ones.
Where people slip in interviews
- Forgetting the +1 in expected-time equations, or adding a +1 to probability equations.
- States that forget too much. For HHT, an extra H from state HH keeps you at HH, not back at start. Check every arrow: “what is the longest useful match now?”
- Wrong boundary. , not 1. You have already paid for the last step.
- Infinite expectations. A fair walk on the whole integer line hits +1 with probability 1, but the expected time is infinite. If the equations have no finite solution, or only a negative one, that is the warning sign.
- Infinite chains, two roots. For “will a walk ever hit −1?” the first-step equation is quadratic, and the correct answer is the smallest nonnegative root (see the last practice problem).
Try these before you open the answer
Warm-upFlip a fair coin until the first head. What is the expected number of flips?Show answer
Warm-upRoll a die until you get two 6s in a row. Expected number of rolls?Show answer
States: start, and “last roll was a 6”.
Same structure as HH with a coin: there it is 2 + 4 = 6, here 6 + 36 = 42.
PatternExpected number of fair coin flips until HHH?Show answer
In general, n heads in a row takes flips.
Penney's gameFlip a fair coin until either HH or TH appears. What is the chance HH comes first?Show answer
First-step thinking settles it: if the first two flips are HH, HH wins. Otherwise a T has appeared before any HH, and the first H after that T completes TH. So HH wins only on HH at the start.
Gambler's ruinFair game, you start with $3 and stop at $0 or $10. Chance of reaching $10, and expected number of rounds?Show answer
InterviewAn ant walks randomly along the edges of a cube, picking each of the 3 edges equally. Expected steps from one corner to the opposite corner?Show answer
Group corners by distance to the target (3, 2, 1, 0):
Grouping by symmetry cut 7 unknowns down to 3. Always look for this.
InterviewRoll a die until every face has appeared. Expected number of rolls?Show answer
State: number of distinct faces seen, k. From state k, a roll is new with probability (6 − k)/6:
This is the coupon collector problem.
GameTwo players take turns rolling a die. The first to roll a 6 wins. What is the chance the first player wins?Show answer
Let a = P(the player about to roll wins). Either they roll a 6 now, or they miss and become the second player:
StretchA walk starts at 0 and moves +1 with probability 0.6, −1 with probability 0.4, forever. What is the chance it ever reaches −1?Show answer
Let r = P(ever reach −1 from 0). Step down (done), or step up and then you must come down twice, each with chance r:
The answer is the smaller root, 2/3. The walk drifts upward, so there is a positive chance it never comes back down. In general it is q/p when p > q.
Every number on this page was checked against a simulation, and the figures compute theirs exactly from the formulas shown, apart from the random games.
Related: Optional stopping and martingales and the birthday problem.