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 all36ordered pairs(d1, d2)equally probable (1/36each). - Compute the expectation deterministically (not via random simulation) and round once at the end to
2decimal places.
Step-by-step walkthrough
Understand the game rules and expected value
We throw two fair six-sided dice (
1through6) simultaneously and collect a dollar payout based on the two faces: - Distinct faces (such as2and5): you win the larger face value (max(2, 5) = +5). - Matching faces (same face on both dice, such as5and5): 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 all36ordered pairs divided by36, rounded at the end to two decimal places.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..6and the navy die independently lands on1..6, forming6 * 6 = 36ordered pairs(die1, die2)of probability1 / 36each:- Throwing a
2and a5happens in two distinct ways —(amber=2, navy=5)or(amber=5, navy=2)— giving probability2 / 36. - Throwing two
5s happens in only one way —(amber=5, navy=5)— giving probability1 / 36.
Averaging only the
21unordered pairs ({1, 1}, {1, 2}, ..., {6, 6}) with equal weight is a classic mistake: it overweights matching throws twofold and yields1.33rather than2.72!- Throwing a
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)Average the exact payoffs over all 36 outcomes
Because there are only
36ordered pairs, two nestedforloops visit every(die1, die2)pair in1..6, accumulate exact integer payouts, divide once by36, 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)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 6payout 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 -12Partition the
36cells into the main diagonal (matching pairs) and off-diagonal L-shaped bands grouped by larger facem:- 6 matching pairs on the diagonal:
(-2) + (-4) + (-6) + (-8) + (-10) + (-12) = -42. - 30 distinct pairs off the diagonal: For each larger face
mfrom2to6, the smaller face takes any of them - 1values[1 .. m - 1]on either die, producing2 * (m - 1)cells paying+meach:m = 2:2 * 2 = +4m = 3:4 * 3 = +12m = 4:6 * 4 = +24m = 5:8 * 5 = +40m = 6:10 * 6 = +60- Distinct-pair subtotal:
4 + 12 + 24 + 40 + 60 = +140.
Combining both parts gives
140 - 42 = 98over36ordered outcomes, so the exact mean is98 / 36 = 49 / 18 = 2.7222..., which rounds to2.72.- 6 matching pairs on the diagonal:
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 run6 * 6 = 36iterations doing constant-time integer comparisons and additions. (Forkdice withsfaces each, exhaustive enumeration visitss^kordered tuples inO(s^k)time.) - Space:
O(1). Only two integer accumulators (total_payoffandnum_outcomes) are kept in memory.
Edge cases
- Exact integer accumulation prior to division: Every single throw's payout is an integer, so
total_payoff = 98incurs zero floating-point rounding error before the single division98 / 36. - Unambiguous cent rounding (
2.7222... -> 2.72):49 / 18 = 2.7222...sits well clear of the half-cent boundary2.725, soround(expected_payoff, 2)cleanly returns2.72. - Numeric
floatreturn type: Return2.72as a Pythonfloat, not a formatted currency string like"$2.72".
Common mistakes
- Averaging the
21unordered pairs equally: Assigning probability1 / 21to both{2, 5}and{5, 5}double-weights matching pairs and yields28 / 21 ≈ 1.33. - Omitting the negative sign on matching pairs: Adding
+(die1 + die2)instead of-(die1 + die2)flips-42to+42and outputs182 / 36 ≈ 5.06. - Subtracting only one die's value
-die1on matching pairs: Losing-5instead of-10on(5, 5)subtracts21instead of42and outputs119 / 36 ≈ 3.31. - Approximating via random trials: Even
10,000simulated throws carry a standard error near0.047(since single-throw payouts swing from-12to+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 the6 x 6payout 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 / 18to-49 / 18 ≈ -2.72(whereas if matching pairs paid only a single face+d, the diagonal would sum to+21against-140for-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 from6^2 = 36pairs to6^3 = 216ordered 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 at216states, though acrosskdice6^kgrows exponentially; when payouts depend only on sorted face counts, you can instead enumerate unordered multisets (collections allowing repeated faces — just56for3dice) 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
kand rerolling the other die across1..6yields conditional expectation3fork = 1,8/3 ≈ 2.67fork = 2,5/2 = 2.5fork = 3or4,8/3 ≈ 2.67fork = 5, and3fork = 6— keeping a1ties keeping a6because hitting(1, 1)only loses2whereas(6, 6)loses12! Takingmax(current_payoff, E(keep die1), E(keep die2))on each initial roll shows you should reroll one die on every matching pair, reroll the2when holding(1, 2)or(2, 1)(lifting expectation from+2to+3), optionally reroll the3on(1, 3)(tied at+3), and stand pat on all other distinct pairs, boosting overall expected return from2.72to475 / 108 ≈ 4.40.
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?
- How does adding a third six-sided die scale the outcome space and affect the cost of computing the exact expectation?
- If you may optionally reroll a single chosen die after viewing the initial pair, which reroll policy maximizes your expected return?
Similar questions
- Knights and Knaves Two-Claim Deductions
- Posterior Probability of Plant A Given k Red Draws
- Expected Die Rolls for Stopping Patterns
- Conditional Age-Bracket Probability Given Cycling
- Goal-Reaching Probability Under Bold Wagering
- Muffin Inventory Sufficiency Probability
More SIG (Susquehanna) interview questions
- Posterior Probability of Plant A Given k Red Draws Medium
- Conditional Age-Bracket Probability Given Cycling Easy
- Probability of Concurrent Bloom Windows Easy Pro
- Posterior Probability Given a Losing Rabbit Easy Pro
- Cookie Assortment Combination Counts Easy Pro
- Streaming Equal-Step Triple Counts Under Updates Medium Pro
- Grid Paths Without Triple Consecutive Steps Easy Pro
- Cat Ownership Puzzle Under Comparative Honesty Rules Easy Pro
- FIFO Inventory Register with Atomic Checkouts Easy Pro
- Count Ants from Total Leg Observations Easy Pro