Expected Payout of a Two-Dice Roll Game

Easy · Statistics & Math · Data Scientist · Online Assessment

Problem Statement

Consider a single-turn dice game played with two fair, mutually independent six-sided dice whose faces are numbered 1 through 6. After throwing both dice simultaneously, your net dollar payout is determined by two rules:

  • Distinct faces (d1 != d2): You win an amount equal to the larger of the two face values, +max(d1, d2).
  • Matching faces (d1 == d2): You lose an amount equal to the combined total of both dice, -(d1 + d2).

Implement calculate_expected_payoff() to compute the exact expected payout of one throw of the two dice and return the result as a float rounded to two decimal places (0.01).

Examples

Example 1

Input: (no arguments)
Output: 2.72

Explanation: On a throw with two distinct faces such as (2, 5) (or (5, 2)), the player receives +max(2, 5) = +5. Summing +max(d1, d2) across all 30 distinct ordered pairs (d1, d2) yields +140, while the 6 matching pairs subtract 42, giving (140 - 42) / 36 = 98 / 36 ≈ 2.72.

Example 2

Input: (no arguments)
Output: 2.72

Explanation: On a matching throw such as (5, 5), the player loses the sum of both faces for a payout of -(5 + 5) = -10. Across all 36 equally likely ordered pairs, the exact mean is 49 / 18 = 2.7222..., which rounds to 2.72.

Constraints

  • Both dice are fair and independent with faces {1, 2, 3, 4, 5, 6}, making all 36 ordered pairs (d1, d2) equally probable (1/36 each).
  • Compute the expectation deterministically (not via random simulation) and round once at the end to 2 decimal places.

Step-by-step walkthrough

  1. Understand the game rules and expected value

    We throw two fair six-sided dice (1 through 6) simultaneously and collect a dollar payout based on the two faces: - Distinct faces (such as 2 and 5): you win the larger face value (max(2, 5) = +5). - Matching faces (same face on both dice, such as 5 and 5): you lose their combined sum (-(5 + 5) = -10).

    The expected value is the long-run average payout per throw. Since every elementary ordered outcome of two fair dice occurs with equal probability 1 / 36, the expected payout equals the sum of payouts across all 36 ordered pairs divided by 36, rounded at the end to two decimal places.

  2. Count all 36 equally likely ordered throws

    To keep every elementary outcome genuinely equiprobable, picture one amber die and one navy die. The amber die lands on 1..6 and the navy die independently lands on 1..6, forming 6 * 6 = 36 ordered pairs (die1, die2) of probability 1 / 36 each:

    • Throwing a 2 and a 5 happens in two distinct ways — (amber=2, navy=5) or (amber=5, navy=2) — giving probability 2 / 36.
    • Throwing two 5s happens in only one way — (amber=5, navy=5) — giving probability 1 / 36.

    Averaging only the 21 unordered pairs ({1, 1}, {1, 2}, ..., {6, 6}) with equal weight is a classic mistake: it overweights matching throws twofold and yields 1.33 rather than 2.72!

  3. Write a helper for one roll's payoff

    Isolating the single-throw payout logic inside roll_payoff(die1, die2) keeps the main enumeration loop clean and readable:

    def roll_payoff(die1: int, die2: int) -> int:
        # Rule 2: rolling a double (both dice show the same face) loses the sum of the two dice.
        if die1 == die2:
            return -(die1 + die2)
        # Rule 1: rolling two different faces wins the larger face value.
        return max(die1, die2)
    
  4. Average the exact payoffs over all 36 outcomes

    Because there are only 36 ordered pairs, two nested for loops visit every (die1, die2) pair in 1..6, accumulate exact integer payouts, divide once by 36, and round to two decimal places:

    def calculate_expected_payoff() -> float:
        # Enumerate all 6 * 6 = 36 equally likely ordered pairs (die1, die2).
        # Treating dice as ordered ensures (2, 5) and (5, 2) are counted as 2 outcomes, while (5, 5) is 1.
        total_payoff = 0
        num_outcomes = 0
    
        for die1 in range(1, 7):
            for die2 in range(1, 7):
                total_payoff += roll_payoff(die1, die2)
                num_outcomes += 1
    
        # Divide and round only once at the very end so integer sums stay exact until formatting.
        expected_payoff = total_payoff / num_outcomes
        return round(expected_payoff, 2)
    
  5. Visualize the full 6x6 payoff grid and verify by hand

    To confirm the exact fraction on paper during an interview, write out the 6 x 6 payout matrix:

    die1 \ die2 |   1    2    3    4    5    6
    ------------+-----------------------------
         1      |  -2   +2   +3   +4   +5   +6
         2      |  +2   -4   +3   +4   +5   +6
         3      |  +3   +3   -6   +4   +5   +6
         4      |  +4   +4   +4   -8   +5   +6
         5      |  +5   +5   +5   +5  -10   +6
         6      |  +6   +6   +6   +6   +6  -12
    

    Partition the 36 cells into the main diagonal (matching pairs) and off-diagonal L-shaped bands grouped by larger face m:

    1. 6 matching pairs on the diagonal: (-2) + (-4) + (-6) + (-8) + (-10) + (-12) = -42.
    2. 30 distinct pairs off the diagonal: For each larger face m from 2 to 6, the smaller face takes any of the m - 1 values [1 .. m - 1] on either die, producing 2 * (m - 1) cells paying +m each:
      • m = 2: 2 * 2 = +4
      • m = 3: 4 * 3 = +12
      • m = 4: 6 * 4 = +24
      • m = 5: 8 * 5 = +40
      • m = 6: 10 * 6 = +60
      • Distinct-pair subtotal: 4 + 12 + 24 + 40 + 60 = +140.

    Combining both parts gives 140 - 42 = 98 over 36 ordered outcomes, so the exact mean is 98 / 36 = 49 / 18 = 2.7222..., which rounds to 2.72.

  6. Complete solution

    from typing import Any, Dict, List, Optional, Set, Tuple
    
    
    def roll_payoff(die1: int, die2: int) -> int:
        # Rule 2: rolling a double (both dice show the same face) loses the sum of the two dice.
        if die1 == die2:
            return -(die1 + die2)
        # Rule 1: rolling two different faces wins the larger face value.
        return max(die1, die2)
    
    
    def calculate_expected_payoff() -> float:
        # Enumerate all 6 * 6 = 36 equally likely ordered pairs (die1, die2).
        # Treating dice as ordered ensures (2, 5) and (5, 2) are counted as 2 outcomes, while (5, 5) is 1.
        total_payoff = 0
        num_outcomes = 0
    
        for die1 in range(1, 7):
            for die2 in range(1, 7):
                total_payoff += roll_payoff(die1, die2)
                num_outcomes += 1
    
        # Divide and round only once at the very end so integer sums stay exact until formatting.
        expected_payoff = total_payoff / num_outcomes
        return round(expected_payoff, 2)
    

    ✅ Submit now — all tests should pass.

Intuition & how it works

Imagine tossing one amber die and one navy die onto a felt table: 6 faces on the amber die paired with 6 independent faces on the navy die create a sample space (the exhaustive set of all elementary outcomes of a random experiment) of 36 equally probable ordered pairs (die1, die2). The expected value (the long-run average payout per throw over infinitely many plays, obtained by weighting each outcome's payout by its probability) reduces to the plain arithmetic mean across all 36 cells of a 6 x 6 grid: 30 distinct-face cells pay +max(die1, die2) while 6 diagonal cells pay -(die1 + die2).

Why do common shortcuts and random simulations fail here? If you list only the 21 unordered pairs like {2, 5} and {5, 5} and average them with equal weight, you treat {5, 5} as just as probable as {2, 5}, even though {2, 5} spans two ordered throws (2, 5) and (5, 2) — dragging the average down to 1.33. And if you rely on Monte Carlo simulation (estimating an expectation by generating thousands of pseudo-random throws instead of summing exact probabilities), sampling noise of a couple of cents will intermittently fail an exact 2.72 check, whereas iterating through all 36 ordered outcomes is instantaneous and exact.

Here is the calculation first on miniature 3-sided dice (1..3, 9 ordered throws) and then on standard 6-sided dice:

Miniature 3-sided check (9 ordered throws):
  Matching:  (1,1)->-2, (2,2)->-4, (3,3)->-6                     => sum = -12
  Distinct:  (1,2),(2,1)->+2 each; (1,3),(3,1),(2,3),(3,2)->+3 each => 4 + 12 = +16
  Expected payout = (16 - 12) / 9 = 4 / 9 ≈ 0.44

Standard 6-sided game (36 ordered throws):
  6 matching throws:   (-2) + (-4) + (-6) + (-8) + (-10) + (-12) = -42
  30 distinct throws:  2*(2) + 4*(3) + 6*(4) + 8*(5) + 10*(6)    = +140
  Expected payout = (140 - 42) / 36 = 98 / 36 = 49 / 18 = 2.7222... -> rounds to 2.72

Complexity

  • Time: O(1). The nested loops always run 6 * 6 = 36 iterations doing constant-time integer comparisons and additions. (For k dice with s faces each, exhaustive enumeration visits s^k ordered tuples in O(s^k) time.)
  • Space: O(1). Only two integer accumulators (total_payoff and num_outcomes) are kept in memory.

Edge cases

  • Exact integer accumulation prior to division: Every single throw's payout is an integer, so total_payoff = 98 incurs zero floating-point rounding error before the single division 98 / 36.
  • Unambiguous cent rounding (2.7222... -> 2.72): 49 / 18 = 2.7222... sits well clear of the half-cent boundary 2.725, so round(expected_payoff, 2) cleanly returns 2.72.
  • Numeric float return type: Return 2.72 as a Python float, not a formatted currency string like "$2.72".

Common mistakes

  • Averaging the 21 unordered pairs equally: Assigning probability 1 / 21 to both {2, 5} and {5, 5} double-weights matching pairs and yields 28 / 21 ≈ 1.33.
  • Omitting the negative sign on matching pairs: Adding +(die1 + die2) instead of -(die1 + die2) flips -42 to +42 and outputs 182 / 36 ≈ 5.06.
  • Subtracting only one die's value -die1 on matching pairs: Losing -5 instead of -10 on (5, 5) subtracts 21 instead of 42 and outputs 119 / 36 ≈ 3.31.
  • Approximating via random trials: Even 10,000 simulated throws carry a standard error near 0.047 (since single-throw payouts swing from -12 to +6), causing frequent cent-level errors.

Follow-up questions

  • What happens to the expected payout if the signs are inverted—winning the two-die sum on matching rolls and losing the larger face value when the dice differ? Every cell (die1, die2) in the 6 x 6 payout matrix becomes the exact negation of its original value (+(die1 + die2) on the diagonal and -max(die1, die2) off the diagonal). By linearity of expectation (the rule that negating every outcome's value negates the overall expected value), the expectation simply flips sign from +49 / 18 to -49 / 18 ≈ -2.72 (whereas if matching pairs paid only a single face +d, the diagonal would sum to +21 against -140 for -119 / 36 ≈ -3.31).
  • How does adding a third six-sided die scale the outcome space and affect the cost of computing the exact expectation? Introducing a third independent die multiplies the ordered sample space by 6, expanding from 6^2 = 36 pairs to 6^3 = 216 ordered triples (once you clarify how two-of-a-kind like (4, 4, 6) is scored relative to three-of-a-kind (4, 4, 4)). Exhaustive enumeration is still instant at 216 states, though across k dice 6^k grows exponentially; when payouts depend only on sorted face counts, you can instead enumerate unordered multisets (collections allowing repeated faces — just 56 for 3 dice) weighted by their multinomial permutation counts.
  • If you may optionally reroll a single chosen die after viewing the initial pair, which reroll policy maximizes your expected return? Keeping a die showing face k and rerolling the other die across 1..6 yields conditional expectation 3 for k = 1, 8/3 ≈ 2.67 for k = 2, 5/2 = 2.5 for k = 3 or 4, 8/3 ≈ 2.67 for k = 5, and 3 for k = 6 — keeping a 1 ties keeping a 6 because hitting (1, 1) only loses 2 whereas (6, 6) loses 12! Taking max(current_payoff, E(keep die1), E(keep die2)) on each initial roll shows you should reroll one die on every matching pair, reroll the 2 when holding (1, 2) or (2, 1) (lifting expectation from +2 to +3), optionally reroll the 3 on (1, 3) (tied at +3), and stand pat on all other distinct pairs, boosting overall expected return from 2.72 to 475 / 108 ≈ 4.40.

Follow-up questions

Similar questions

More SIG (Susquehanna) interview questions

All 44 SIG (Susquehanna) questions →