Expected Coin Flips to Observe Heads Then Tails
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests ability to model a probabilistic process with states and solve expectation equations.
You repeatedly toss a fair coin. You stop as soon as you see Heads immediately followed by Tails (the pattern HT appearing in consecutive flips).
Write a function that returns the expected number of coin flips needed until this pattern first occurs.
Note: The answer should be computed analytically, not by simulation.
Constraints
- The coin is fair: P(Heads) = P(Tails) = 0.5
- Flips are independent
- Return the exact expected value as a float
Examples
Example 1
Input: expected_flips_for_ht()
Output: 4
Example 2
Input: expected_flips_for_ht()
Output: 4
Hints
Hint 1
Track what matters: whether your most recent flip was Heads, since that determines how close you are to completing the pattern.
Hint 2
Set up equations for the expected flips remaining from each state. Remember to add 1 for the flip you're about to make.
Hint 3
Once you flip Heads, you stay one step away from finishing until you eventually flip Tails. What does that tell you about the expected wait from that state?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Model this as a Markov process by tracking the relevant history: what was the last flip? We only care whether the most recent flip was Heads, because that determines proximity to completing the HT pattern.
Define two states:
- Start state: No useful progress (either we just began, or the last flip was Tails with no pending Heads).
- After-H state: The most recent flip was Heads, so we're one Tails away from finishing.
Let E_start be the expected number of flips needed from the start state, and E_after_H be the expected flips needed after we've just seen Heads.
Setting Up the Equations
From the After-H state:
- With probability 0.5, we flip Tails and complete the pattern HT (0 additional flips needed after this one).
- With probability 0.5, we flip Heads again and remain in the After-H state (the new H is still our "last flip").
This gives:
E_after_H = 1 + 0.5 × 0 + 0.5 × E_after_H
Solving:
0.5 × E_after_H = 1
E_after_H = 2
From the Start state:
- With probability 0.5, we flip Heads and move to the After-H state.
- With probability 0.5, we flip Tails and stay in the Start state.
This gives:
E_start = 1 + 0.5 × E_after_H + 0.5 × E_start
Substituting E_after_H = 2:
E_start = 1 + 0.5 × 2 + 0.5 × E_start
0.5 × E_start = 2
E_start = 4
Why This Is Correct
The key insight is that once we flip Heads, we can never lose progress. Another Heads keeps us one step away from finishing, and eventually we'll flip Tails. This makes the problem decompose cleanly:
- Wait for the first Heads: expected 2 flips (geometric distribution with p=0.5).
- After that, wait for the first Tails: expected 2 flips.
- Total: 2 + 2 = 4 flips.
Complexity
- Time: O(1) — analytical solution with no iteration
- Space: O(1) — constant storage
def expected_flips_for_ht() -> float:
# State-based approach:
# Let E_start = expected flips from the beginning (or after seeing T with no H before it)
# Let E_after_H = expected flips after seeing H as the most recent flip
#
# From the start state:
# - Flip H with probability 0.5 → move to "after H" state
# - Flip T with probability 0.5 → stay in start state
# E_start = 1 + 0.5 * E_after_H + 0.5 * E_start
#
# From the "after H" state:
# - Flip T with probability 0.5 → done (0 more flips needed)
# - Flip H with probability 0.5 → stay in "after H" state
# E_after_H = 1 + 0.5 * 0 + 0.5 * E_after_H
#
# Solve E_after_H:
# E_after_H = 1 + 0.5 * E_after_H
# 0.5 * E_after_H = 1
# E_after_H = 2
#
# Solve E_start:
# E_start = 1 + 0.5 * 2 + 0.5 * E_start
# 0.5 * E_start = 1 + 1
# E_start = 4
return 4.0