Qm
Core

First-Step Analysis: Conditioning on What Happens Next

Most "expected number of flips/steps until X" puzzles look intractable until you ask one question: what happens after the very first move, and can I write the answer in terms of itself?

Prerequisites: Conditional Expectation

You flip a fair coin repeatedly. What's the expected number of flips until you see heads? Everyone knows the answer is 2, but why, in a way that generalises to harder versions where the intuition runs out? What's the expected number of flips until you see two heads in a row? That one stops most people cold if they try to reason about it directly.

Try the two-heads-in-a-row version yourself before reading on, count out a few sequences by hand if it helps.

The trap: trying to enumerate every possible sequence

The direct approach, list every sequence of flips that ends in the target pattern, weight each by its probability and length, and sum, is exactly what a computer would do and exactly what a human shouldn't. The sequences form an infinite, awkwardly-structured set (HH, THH, TTHH, HTHH, …) and summing an infinite series by hand under interview pressure is a losing game.

The fix is to stop thinking about the whole future and instead ask: what happens on just the first step, and where does that leave me?

First-step analysis: define EE as the expected number of steps to the target, condition on the outcome of the very first step, and write EE as a weighted average of "1 (for the step you just took) plus whatever comes next", where "whatever comes next" is often EE itself, or a new related unknown. You end up solving an algebraic equation instead of summing a series.

Worked example: expected flips to first heads, the simple case

Let EE be the expected number of flips until the first heads. On the very first flip, two things can happen: heads, with probability 1/2, in which case you're done after exactly 1 flip; or tails, with probability 1/2, in which case you've used 1 flip and are now, because the coin has no memory, in exactly the same situation you started in, needing EE more flips on average.

E=12(1)+12(1+E).E = \frac12(1) + \frac12(1+E).

In words: half the time you finish in one flip; half the time you spend one flip and then face the identical problem again. Solving: E=12+12+12E12E=1E=2E = \frac12 + \frac12 + \frac12 E \Rightarrow \frac12 E = 1 \Rightarrow E = 2. No infinite sum, just one equation with one unknown, because the recursive structure of the problem let EE appear on both sides.

Worked example: expected flips until two heads in a row

Now the harder version. The key realisation is that a single state isn't enough, you need to track how close you are, so define two unknowns: E0E_0, the expected number of additional flips needed when your current streak of consecutive heads is 0 (including the very start, or right after a tails), and E1E_1, the expected number of additional flips needed when you currently have exactly one head in a row.

From state 0: flip once. Heads (prob 1/2) moves you to state 1, having used 1 flip. Tails (prob 1/2) keeps you in state 0, having used 1 flip.

E0=1+12E1+12E0.E_0 = 1 + \frac12 E_1 + \frac12 E_0.

From state 1: flip once. Heads (prob 1/2) means you've hit two in a row, done. Tails (prob 1/2) breaks the streak, sending you back to state 0.

E1=1+12(0)+12E0.E_1 = 1 + \frac12(0) + \frac12 E_0.

In words: each equation says "spend one flip, then land in whichever state the coin sends you to, weighted by its chance." Substitute the second into the first: E0=1+12(1+12E0)+12E0=1+12+14E0+12E0E_0 = 1 + \frac12\left(1+\frac12 E_0\right) + \frac12 E_0 = 1 + \frac12 + \frac14 E_0 + \frac12 E_0, so E034E0=32E_0 - \frac34 E_0 = \frac32, giving 14E0=32\frac14 E_0 = \frac32, so E0=6E_0 = 6. It takes 6 flips on average to see HH, noticeably more than double the 2 flips for a single head, because a broken streak forces you all the way back to state 0, not partway.

state 0 state 1 HH done H, 1/2 T, 1/2 T, 1/2 (back to state 0) H, 1/2
Each unknown E is defined as "expected steps remaining from this state", the equations fall straight out of the arrows leaving each node.

The trap in multi-state problems is forgetting that a broken streak doesn't reset to a fresh start unless the pattern has no internal repetition. For HH it resets cleanly to state 0. For a pattern like HTH, breaking after "HT" and seeing another H doesn't send you back to nothing, it sends you partway back in, because the tail end of what you've seen might already match the start of the target. Always check for this kind of overlap before assuming a clean reset.

The transferable technique

First-step analysis turns "sum an infinite or awkward set of futures" into "write one equation per state, using the fact that the unknown reappears on the right-hand side." The steps are always the same: define enough states to capture everything relevant to the future (often "how much progress have I made"), write one equation per state by conditioning on the very next step, and solve the resulting linear system. This is the exact machinery behind gambler's-ruin durations, random-walk return probabilities, and every "expected time until pattern X" question, recognise the shape and the algebra does the rest.

Discussion

Sign in to join the discussion · reading is open to everyone

💡 Discussion rules

  1. Ask and answer about this concept. Off-topic gets removed.
  2. No homework dumps. Show what you tried first.
  3. Corrections are welcome. Cite a source when you claim an error.

Loading discussion…

Related concepts

Practice in interviews

Further reading

  • Blitzstein & Hwang, Introduction to Probability, ch. 9
ShareTwitterLinkedIn