KatInterview

Best-of-Five: Probability All Five Games Are Played

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashingprobabilitycombinatoricsmath

Last 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.