Three-Player Fair Die Playoff Probability

Medium · Statistics & Math · Software Engineer · HR Screen

Problem Statement

Three contestants — you (A) and two rivals (B and C) — compete using unbiased standard dice numbered 1 through 6. Rivals B and C have completed their opening throw, and each landed on 4.

Now you make a single opening throw:

  • Above 4 (5 or 6): you claim an outright victory on the spot.
  • Below 4 (1, 2, or 3): you are eliminated on the spot and lose.
  • Equal to 4: all three contestants enter a sudden-death playoff. In every playoff round, A, B, and C throw their dice together; whenever two or all three contestants share the round's maximum value, everyone throws again, continuing until some round produces a single solitary maximum. Whoever rolled that solitary maximum takes the win.

Implement calculate_win_chance() -> float to return the exact overall probability that you end up winning the game.

Examples

Example 1

Input: (no arguments)
Call: calculate_win_chance()
Output: 0.38888888888888884

Out of 6 equally likely opening faces, 5 and 6 (2/6) win outright, 1..3 (3/6) lose outright, and 4 (1/6) starts a completely symmetric three-contestant playoff where each contestant wins with probability 1/3. Combining both winning paths gives 2/6 + (1/6) * (1/3) = 7/18 ≈ 0.38888888888888884.

Example 2

Trajectory 1: You open with 6 -> exceeds both 4s -> immediate victory.
Trajectory 2: You open with 2 -> trails both 4s -> immediate defeat.
Trajectory 3: You open with 4 -> playoff round 1 rolls (A=5, B=5, C=2) [shared high 5, replay]
                               -> playoff round 2 rolls (A=6, B=3, C=5) [solitary high 6, A wins].

Note that a two-way tie at the top during a playoff round (such as (5, 5, 2)) still triggers a full three-contestant replay because there is no solitary highest roll yet.

Constraints

  • Accepts zero arguments and returns a Python float.
  • Evaluated within relative floating-point tolerance 1e-6, though exact IEEE-754 precision (7 / 18) is expected.
  • Runs deterministically in O(1) time and O(1) space.

Step-by-step walkthrough

  1. Restate the rules and trace your first roll

    Rivals B and C each opened with 4 on an unbiased 1..6 die. Now you (A) throw once, and each of the 6 equally probable outcomes (1/6 apiece) routes the match into one of three branches:

    • Throw 5 or 6 (2 faces): your score exceeds 4, claiming victory on the spot.
    • Throw 1, 2, or 3 (3 faces): your score trails 4, eliminating you right away.
    • Throw 4 (1 face): all three contestants sit deadlocked at 4, launching simultaneous three-way playoff rounds that repeat any top-end tie (such as (5, 5, 2)) until a round produces one solitary maximum (such as (6, 3, 5)).
    Your first roll (6 equally likely faces, 1/6 each)
      +-- 5 or 6  (prob 2/6) --> WIN immediately
      +-- 4       (prob 1/6) --> 3-player simultaneous reroll until unique max
      +-- 1, 2, 3 (prob 3/6) --> LOSE immediately
    

    The judge calls calculate_win_chance() with no arguments and expects your exact overall probability of winning as a float.

  2. Split the total win chance across the branches

    Because your first roll lands in exactly one of those three branches, we can use the law of total probability (a rule that finds an event's overall chance by adding up its chances across separate, non-overlapping cases). In each branch, we multiply the chance of entering that branch by the conditional probability of winning from there (the probability you win assuming that branch already happened):

    P(win) = P(roll 5 or 6) * 1
           + P(roll 4)      * P(win given reroll)
           + P(roll 1..3)   * 0
    
           = 2/6 + (1/6) * P(win given reroll)
    

    Everything now comes down to one number: P(win given reroll), your chance of eventually winning once the three-player reroll begins.

  3. Key idea: symmetry solves the reroll instantly

    Once you roll a 4, the earlier rolls no longer affect anything — the scoreboard is wiped clean and A, B, and C start a fresh simultaneous game. Every round, all three players roll the same fair six-sided die under the exact same rule.

    If you swapped your name tag with B or C before the reroll started, nothing about the game would change. This is a symmetry argument (reasoning that players in identical positions under identical rules must have equal probabilities of winning). To turn equal probabilities into 1/3, we check two quick facts:

    1. The game is guaranteed to finish (probability 1). In any reroll round, there is a positive chance of a single winner (for example, (6, 1, 1) gives A the win right away). Because independent rounds keep trying, the chance of tying forever shrinks to 0.
    2. Every finished game has exactly one winner. The game only stops when there is a unique highest face, so two players can never win at the same time.

    Since P(A wins reroll) + P(B wins reroll) + P(C wins reroll) = 1 and all three probabilities are equal, each player wins the reroll with probability 1/3. Plugging 1/3 back into our branch formula gives:

    P(win) = 2/6 + (1/6) * (1/3)
           = 6/18 + 1/18
           = 7/18
           ≈ 0.38888888888888884
    
  4. Verify symmetry by counting all 216 reroll outcomes

    If the symmetry shortcut feels too magical, we can double-check 1/3 by counting one reroll round directly. When A, B, and C each roll a six-sided die, there are 6 * 6 * 6 = 216 equally likely triples (a, b, c).

    When is A the unique highest roller? Only when both B and C roll strictly below A's face a (so b has a - 1 choices and c has a - 1 choices):

    A's roll (a) | Valid faces for B and C | Triples where A is unique max
    -------------+-------------------------+------------------------------
         1       | none                    | 0 * 0 =  0
         2       | {1}                     | 1 * 1 =  1
         3       | {1, 2}                  | 2 * 2 =  4
         4       | {1, 2, 3}               | 3 * 3 =  9
         5       | {1, 2, 3, 4}            | 4 * 4 = 16
         6       | {1, 2, 3, 4, 5}         | 5 * 5 = 25
    -------------+-------------------------+------------------------------
    Total for A                            |         55 triples
    

    Now account for all 216 triples in one reroll round:

    • A wins this round: 55 triples.
    • B wins this round: 55 triples (same table with B in front).
    • C wins this round: 55 triples (same table with C in front).
    • No unique highest face (reroll again): 216 - (55 + 55 + 55) = 51 triples (6 three-way ties like (4, 4, 4) plus 45 two-way ties at the top like (6, 6, 3)).

    Let w be A's probability of eventually winning the reroll. Using first-step analysis (writing an equation for w based on what happens on the very first reroll round): with probability 55/216 you win immediately, and with probability 51/216 everyone ties for the lead and restarts the exact same reroll game with win probability w:

    w = 55/216 + (51/216) * w
    (1 - 51/216) * w = 55/216
    (165/216) * w    = 55/216
    w = 55 / 165 = 1 / 3
    

    We can also verify 55 / 165 = 1/3 with a short loop over all 216 triples:

    def tie_break_win_chance() -> float:
        a_unique_top = 0      # triples where A alone has the highest roll
        someone_unique_top = 0  # triples that end the reroll round
        for a in range(1, 7):
            for b in range(1, 7):
                for c in range(1, 7):
                    highest = max(a, b, c)
                    if [a, b, c].count(highest) == 1:
                        someone_unique_top += 1
                        if a == highest:
                            a_unique_top += 1
        # Tied rounds simply replay from scratch, so A wins 55 of the 165 decisive triples.
        return a_unique_top / someone_unique_top
    
  5. Sanity-check against common probability traps

    Before coding the final return value, verify why 7/18 ≈ 0.3889 sits right where intuition says it must:

    • Lower than 1/2 (0.500): half the die faces (1, 2, 3) lose immediately on your first roll.
    • Higher than 1/3 (0.333): rolling 4 gives you a second life instead of an automatic loss.
    • Why 5/12 ≈ 0.417 is wrong: 2/6 + (1/6) * (1/2) treats the reroll as a 2-player coin flip, forgetting there are 3 symmetric players competing for the top roll.
    • Why 2/6 + (1/6) * (55/216) ≈ 0.376 is wrong: 55/216 counts winning on the first reroll round only and treats the 51 tied triples as losses instead of replays.
  6. Complete solution

    In code, we name each branch's probability explicitly so every piece of the 7/18 calculation is self-explanatory.

    from typing import Any, Dict, List, Optional, Set, Tuple
    
    
    def calculate_win_chance() -> float:
        # Total equally likely faces on your fair six-sided die (1 through 6).
        die_sides = 6
    
        # Phase 1: Outcomes resolved immediately on your single initial roll.
        # Both opponents B and C showed 4, so rolling 5 or 6 (2 faces) wins outright.
        win_immediately_prob = 2 / die_sides
        # Rolling 1, 2, or 3 (3 faces) is strictly below 4 and loses immediately (adds 0).
    
        # Phase 2: Outcome that triggers the simultaneous three-way reroll.
        # Rolling exact 4 (1 face) creates a three-way tie at 4.
        tie_on_first_roll_prob = 1 / die_sides
    
        # Phase 3: Conditional probability of winning once the three-way reroll starts.
        # From here, A, B, and C reroll identical fair dice under identical rules until
        # one unique highest face appears, so symmetry gives each of the 3 players 1/3.
        win_given_reroll_prob = 1 / 3
    
        # Combine disjoint winning paths: win outright OR (tie first AND win the reroll).
        total_win_prob = win_immediately_prob + tie_on_first_roll_prob * win_given_reroll_prob
        return total_win_prob
    

    ✅ Submit now — all tests should pass.

Intuition & how it works

Picture walking up to an arcade game where two opponents, B and C, are tied on the leaderboard with a score of 4. You get one shot: beat 4 and you take the trophy on the spot, fall short of 4 and you are out, or match 4 and the machine wipes the leaderboard so all three of you enter a sudden-death playoff on completely equal footing. Once that sudden-death playoff begins, the earlier 4s no longer matter — three players roll identical fair dice under identical rules until a single highest number appears, so by symmetry (when identical rules treat every player interchangeably, every player must have the exact same chance of winning) each of the three players wins the playoff with probability 1/3.

Why do quick guesses or brute-force simulations fall short? Counting a 4 as a loss (2/6 = 1/3) throws away the sudden-death playoff, while treating the tie-breaker as a 50/50 coin flip (2/6 + 1/12 = 5/12) forgets you are facing two opponents at once, not one. Trying to sum round-by-round reroll trees by hand gets tangled in infinite loops of ties, and Monte Carlo simulation (estimating a probability by running thousands of random trials on a computer and dividing wins by total trials) produces slightly different approximate decimals each run and cannot guarantee 1e-6 precision.

Here is the complete breakdown across all 6 equally likely faces of your initial roll:

Face 1 -> below 4 -> lose immediately        (contributes 0)
Face 2 -> below 4 -> lose immediately        (contributes 0)
Face 3 -> below 4 -> lose immediately        (contributes 0)
Face 4 -> ties 4  -> 1/6 chance * 1/3 reroll (contributes 1/18)
Face 5 -> beats 4 -> win immediately         (contributes 1/6 = 3/18)
Face 6 -> beats 4 -> win immediately         (contributes 1/6 = 3/18)
--------------------------------------------------------------------
Total win probability = 0 + 0 + 0 + 1/18 + 3/18 + 3/18 = 7/18 ≈ 0.3889

Complexity

  • Time: O(1). We compute a fixed arithmetic expression using three constant probabilities (2/6, 1/6, and 1/3), doing a constant handful of divisions and additions with no loops.
  • Space: O(1). We store only five scalar numbers in local variables regardless of anything else.

Edge cases

  • Your initial roll is 4: This neither wins nor loses immediately; it enters the simultaneous three-player reroll where your conditional win chance is 1/3.
  • Two players tie for the highest face during a reroll (such as 5, 5, 2): Because 5 is shared by two players, there is no solitary maximum, so nobody wins yet and all three players reroll again (45 of the 216 triples do this).
  • All three players roll the exact same face during a reroll (such as 5, 5, 5): Again there is no unique highest face (6 of the 216 triples), triggering another full reroll.
  • Arbitrarily long reroll streaks: Although players could tie many times in a row, the probability of tying k reroll rounds in a row is (51/216)^k, which shrinks toward 0 rapidly, so the game finishes with probability 1.
  • Return type: The judge checks for a standard Python float (0.38888888888888884), not None or a fractions.Fraction object.

Common mistakes

  • Treating an initial 4 as an immediate loss: Returning 2/6 = 1/3 ≈ 0.3333 ignores the 1/18 extra win chance from the tie-breaker.
  • Using 1/2 instead of 1/3 for the reroll: Thinking "either I win the tie-break or I don't" ignores that three symmetric players share the 100% pool of winning outcomes, giving 1/3 each rather than 1/2.
  • Forgetting that tied reroll rounds repeat: In a single reroll round, you are the unique highest roller in 55 of 216 triples, so plugging in 55/216 instead of 55/165 = 1/3 wrongly treats the 51 tied triples as losses and returns ≈ 0.3758.
  • Forgetting to weight the reroll by 1/6: 1/3 is a conditional probability (your chance of winning given that you rolled a 4 first); you must multiply it by the 1/6 chance of actually rolling that 4.
  • Returning a random simulation estimate: Random sampling has standard error roughly 1 / sqrt(N), which takes millions of slow trials in Python just to get reliably within 1e-4, let alone 1e-6.

Alternative approaches

  • Direct enumeration of the 216 reroll triples: Loop over all 6 * 6 * 6 = 216 outcomes (a, b, c) of one reroll round. Player A is strictly highest in 55 triples (0^2 + 1^2 + 2^2 + 3^2 + 4^2 + 5^2), and some player is strictly highest in 3 * 55 = 165 triples. Conditioning on the round that finally ends the game gives 55 / 165 = 1/3 in O(6^3) = O(1) steps without relying on symmetry.
  • First-step analysis / geometric series: First-step analysis means writing an equation for an unknown probability w by breaking down all outcomes of the very next step: w = 55/216 + (51/216) * w, which solves to w = 55/165 = 1/3. Equivalently, summing over 0, 1, 2, ... tied rounds before your winning round forms a geometric series (an infinite sum a + a*r + a*r^2 + ... = a / (1 - r) where each term multiplies by a fixed ratio r with |r| < 1), giving (55/216) / (1 - 51/216) = 1/3.

Follow-up questions

  • How does the formula generalize when k opponents are tied at face v on a fair s-sided die? Assuming s >= 2 and 1 <= v <= s, you beat v outright on s - v faces (probability (s - v) / s) and tie v on 1 face (probability 1 / s). Because s >= 2, the (k + 1)-player simultaneous reroll finishes with probability 1 and treats all k + 1 players identically, so you win the reroll with probability 1 / (k + 1), giving total win probability (s - v) / s + 1 / (s * (k + 1)).
  • Does your probability of winning change if rerolls eliminate lower rollers instead of keeping all three players in? No — your overall win probability is still 7/18! Once you roll 4 to enter the three-player playoff, an elimination rule (for instance, (5, 5, 2) knocks out the 2 and leaves the two 5s in a head-to-head reroll) still treats A, B, and C completely symmetrically and still produces one eventual winner, so each player's playoff win chance remains 1/3 (55/216 outright + (30/216) * (1/2) via a two-way playoff + (6/216) * w = w also solves to w = 1/3).
  • Conditioned on entering the tiebreak, what is the expected number of simultaneous reroll rounds until a winner emerges? Each reroll round produces a solitary winner independently with probability p = 165/216 = 55/72, so the number of reroll rounds follows a geometric distribution (the distribution counting how many independent tries of success probability p are needed until the first success, whose average is 1 / p). Thus the expected number of reroll rounds is 216 / 165 = 72 / 55 ≈ 1.31 rounds.

Follow-up questions

Similar questions

More Jane Street interview questions

All 47 Jane Street questions →