Best-of-Five: Probability All Five Games Are Played
Arrays & HashingAuto-graded · 14 test casesLast updated
Tests whether a candidate can derive the closed-form probability that a best-of-five series requires all five games, using combinatorial reasoning.
Best-of-Five: Probability All Five Games Are Played
Two competitors face each other in a best-of-five series. Each individual game is independent, and one competitor (call them Player A) wins any single game with probability p. The other competitor wins with probability 1 - p. The series ends as soon as one player accumulates three wins.
Let X be the total number of games played before the series concludes. Because a winner must reach three wins, X can only take the values 3, 4, or 5.
Your task is to return the exact probability that the series goes the full distance — that is, Pr(X = 5) — as a function of p.
Constraints
0.0 <= p <= 1.0- The result must be returned as a
float. - All individual games are independent of each other.
- The series stops immediately once either player reaches three wins; no extra games are played.
Examples
Example 1
Input: series_goes_to_five(0.5)
Output: 0.375
Example 2
Input: series_goes_to_five(0.7)
Output: 0.2646
Hints
Hint 1
Think about what the scoreline must look like after exactly four games in order for a fifth game to be necessary. What does that force both players' win counts to be?
Hint 2
Rather than tracing every possible game-by-game path, consider treating the first four games as if they were always played. The number of wins Player A accumulates in four independent games follows a well-known distribution.
Hint 3
The answer is a degree-4 polynomial in p that is symmetric around p = 0.5 and reaches its maximum value of 3/8 at that point. Use that as a sanity check on your formula.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key observation
The series reaches a fifth game if and only if the score after four games is exactly 2–2. If either player held three wins at any earlier point, the series would already be over. Conversely, a 2–2 scoreline means neither player is done, so a decisive fifth game must be played.
Why we can reason about four games freely
Even though the series can end before four games are complete, the event "score is 2–2 after four games" is equivalent to "Player A wins exactly 2 of the first four games." We can imagine those four games are always played (a thought experiment that does not affect the probability). Since each game is independent, the number of A-wins in four games is a Binomial(4, p) random variable.
Deriving the formula
$$ \Pr(X = 5) = \Pr(\text{A wins exactly 2 of 4 games}) = \binom{4}{2} p^2 (1-p)^2 = 6p^2(1-p)^2 $$
Sanity checks
At p = 0.5: 6 * 0.25 * 0.25 = 0.375. The full distribution at p = 0.5 is Pr(3) = 0.25, Pr(4) = 0.375, Pr(5) = 0.375, which sums to 1. ✓
The formula is symmetric in p and 1-p (as expected — relabeling the players should not change whether five games are played). It is maximized at p = 0.5 and approaches 0 as p → 0 or p → 1 (a dominant player ends things quickly). ✓
Complexity
- Time: O(1) — three arithmetic operations.
- Space: O(1).
Reference solution
def series_goes_to_five(p: float) -> float:
# A fifth game is needed if and only if the score stands at 2-2
# after four games — neither player has yet reached three wins.
# The number of games won by Player A in four independent games
# follows Binomial(4, p), so:
# Pr(X = 5) = Pr(A wins exactly 2 of the first 4 games)
# = C(4,2) * p^2 * (1-p)^2
# = 6 * p^2 * (1-p)^2
return 6 * (p ** 2) * ((1 - p) ** 2)
Common mistakes
- Counting only one player's winning path: Writing
6 * p^3 * (1-p)^2counts only the sequences where A wins the fifth game. Adding B's mirror sequences gives6p^3(1-p)^2 + 6p^2(1-p)^3 = 6p^2(1-p)^2(p + (1-p)) = 6p^2(1-p)^2. The compact form accounts for both players automatically. - Using
C(5,3): This counts ordered sequences of five games where one player wins exactly three, but it does not enforce the "series stops at the first player to reach three wins" rule. It overcounts paths where a player reaches three wins before game 5. - Off-by-one on the binomial: The correct base is four games, not five.