Quant interview toolkit · Markov chains

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.

1Pick states that remember just enough For “HH”, you only need to know whether the last flip was H. The rest of the history doesn’t matter.
2One unknown per state: ese_s or psp_s ese_s is the expected number of steps left from s, psp_s the chance of winning from s. You are solving for every starting point at once. That is what makes the equations close up.
3es=1+∑tP(s→t) etps=∑tP(s→t) pte_s = 1 + \sum_t P(s \to t)\, e_t \qquad p_s = \sum_t P(s \to t)\, p_t Condition on where the first step takes you. The “+1” pays for that step. Probabilities have no +1.
4Boundaries: edone=0e_{\text{done}} = 0, pwin=1p_{\text{win}} = 1, plose=0p_{\text{lose}} = 0 Without boundaries the system has no unique answer. They anchor it.
Warm-up · one die

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.

e=1+16⋅0+56 e⟹16e=1⟹e=6e = 1 + \tfrac16 \cdot 0 + \tfrac56\, e \quad\Longrightarrow\quad \tfrac16 e = 1 \quad\Longrightarrow\quad e = 6

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.

Example 1 · coin patterns

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:

estart=1+12 eH+12 estarteH=1+12⋅0+12 estart\begin{aligned} e_{\text{start}} &= 1 + \tfrac12\, e_{H} + \tfrac12\, e_{\text{start}} \\ e_{H} &= 1 + \tfrac12 \cdot 0 + \tfrac12\, e_{\text{start}} \end{aligned}

Substitute the second into the first: estart=1+12(1+12estart)+12estarte_{\text{start}} = 1 + \tfrac12(1 + \tfrac12 e_{\text{start}}) + \tfrac12 e_{\text{start}}, so 14estart=32\tfrac14 e_{\text{start}} = \tfrac32 and estart=6e_{\text{start}} = 6.

Now “HT”. From state H, a T finishes and another H keeps you in H: you never lose your progress. So eH=1+12eHe_H = 1 + \tfrac12 e_H gives eH=2e_H = 2, and estart=4e_{\text{start}} = 4. Same probability per attempt, different answer, because of what happens when you fail. Pick a pattern below and watch the fall-back arrows.

Pattern
the flip that extends the matchthe other flip: fall back to the longest part still matched

First-step equations

Solution

Hover a state in the diagram to see its expected remaining flips.

exact E[flips], first-step analysis
simulated mean
simulated median

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 2k2^k 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.

Example 2 · probabilities

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 PiP_i be the chance you reach N first. Conditioning on the first round:

Pi=p Pi+1+q Pi−1,P0=0, PN=1P_i = p\, P_{i+1} + q\, P_{i-1}, \qquad P_0 = 0,\ P_N = 1

Rearranging gives Pi+1−Pi=qp (Pi−Pi−1)P_{i+1} - P_i = \tfrac{q}{p}\,(P_i - P_{i-1}): the steps between neighbours form a geometric sequence. Summing them and using PN=1P_N = 1:

Pi=1−(q/p)i1−(q/p)N(p≠q),Pi=iN(p=q=12)P_i = \frac{1 - (q/p)^i}{1 - (q/p)^N} \quad (p \ne q), \qquad P_i = \frac{i}{N} \quad (p = q = \tfrac12)

The expected length of the game uses the same equation with a +1, Di=1+p Di+1+q Di−1D_i = 1 + p\,D_{i+1} + q\,D_{i-1} with D0=DN=0D_0 = D_N = 0. For a fair game the answer is Di=i (N−i)D_i = i\,(N - i).

p (win a round)0.50target N10start i3

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.

Scaling up

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:

e=1+Q e⟹(I−Q) e=1\mathbf e = \mathbf 1 + Q\,\mathbf e \quad\Longrightarrow\quad (I - Q)\,\mathbf e = \mathbf 1
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.]

(I−Q)−1(I - Q)^{-1} 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. edone=0e_{\text{done}} = 0, 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).
Practice

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
e=1+12⋅0+12 e⟹e=2e = 1 + \tfrac12 \cdot 0 + \tfrac12\, e \quad\Longrightarrow\quad e = 2
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”.

e0=1+16e1+56e0e1=1+16⋅0+56e0⟹e0=42\begin{aligned} e_0 &= 1 + \tfrac16 e_1 + \tfrac56 e_0 \\ e_1 &= 1 + \tfrac16 \cdot 0 + \tfrac56 e_0 \end{aligned} \quad\Longrightarrow\quad e_0 = 42

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
e0=1+12e1+12e0e1=1+12e2+12e0e2=1+12⋅0+12e0⟹e0=14\begin{aligned} e_0 &= 1 + \tfrac12 e_1 + \tfrac12 e_0 \\ e_1 &= 1 + \tfrac12 e_2 + \tfrac12 e_0 \\ e_2 &= 1 + \tfrac12 \cdot 0 + \tfrac12 e_0 \end{aligned} \quad\Longrightarrow\quad e_0 = 14

In general, n heads in a row takes 2n+1−22^{n+1} - 2 flips.

Penney's gameFlip a fair coin until either HH or TH appears. What is the chance HH comes first?
Show answer
P(HH first)=14P(\text{HH first}) = \tfrac14

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
P3=310=0.3,D3=3⋅(10−3)=21P_3 = \frac{3}{10} = 0.3, \qquad D_3 = 3 \cdot (10 - 3) = 21
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):

e3=1+e2e2=1+23e1+13e3e1=1+13⋅0+23e2⟹e3=10\begin{aligned} e_3 &= 1 + e_2 \\ e_2 &= 1 + \tfrac23 e_1 + \tfrac13 e_3 \\ e_1 &= 1 + \tfrac13 \cdot 0 + \tfrac23 e_2 \end{aligned} \quad\Longrightarrow\quad e_3 = 10

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:

ek=1+k6ek+6−k6ek+1⟹ek−ek+1=66−ke_k = 1 + \tfrac{k}{6} e_k + \tfrac{6-k}{6} e_{k+1} \quad\Longrightarrow\quad e_k - e_{k+1} = \frac{6}{6-k}e0=6(1+12+13+14+15+16)=14.7e_0 = 6\left(1 + \tfrac12 + \tfrac13 + \tfrac14 + \tfrac15 + \tfrac16\right) = 14.7

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:

a=16+56 (1−a)⟹a=611≈0.545a = \tfrac16 + \tfrac56\,(1 - a) \quad\Longrightarrow\quad a = \tfrac{6}{11} \approx 0.545
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:

r=0.4+0.6 r2⟹r=1 or r=23r = 0.4 + 0.6\, r^2 \quad\Longrightarrow\quad r = 1 \ \text{or}\ r = \tfrac23

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.