Three-Dice Payout: Expected Value by Match Type
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests ability to count outcome classes for three N-sided dice and combine probability-weighted payoffs into an expected value, including a sign-flip variant.
Three-Dice Payout: Expected Value by Match Type
You toss three independent, fair dice, each with faces numbered 1 through N. Depending on what the three dice show, you collect a reward according to the table below.
| Outcome | Description | Reward |
|---|---|---|
| Triple | All three dice show the same face | +A dollars |
| Pair | Exactly two dice show the same face (the third differs) | +B dollars |
| All different | No two dice share a face | See below |
There are two versions of the game that differ only in the all-different outcome:
- Version 1 (pays): you receive +C dollars when all three faces differ.
- Version 2 (costs): you pay C dollars (i.e., collect −C dollars) when all three faces differ.
Compute the expected payoff of a single roll for both versions and return them as a list [version_1, version_2].
How to think about it
Count how many of the N³ equally likely outcomes fall into each class, divide by N³ to get probabilities, then form the probability-weighted average for each version.
Constraints
- 2 ≤ N ≤ 10,000
- 0 ≤ A, B, C ≤ 10,000 (amounts are non-negative; the version of the game determines the sign on C)
- There is no entry fee — the only cash flows are the rewards/costs in the table
- "Exactly two the same" excludes the triple case
- Probabilities for the three classes must sum to 1; verify this as a sanity check
- Round only at the end if rounding is needed; return full floating-point precision
Examples
Example 1
Input: dice_match_expected_payoff(6, 100, 20, 5)
Output: [13.88888888888889, 8.333333333333332]
Example 2
Input: dice_match_expected_payoff(2, 50, 10, 7)
Output: [20, 20]
Example 3
Input: dice_match_expected_payoff(10, 12.5, 4.5, 3.25)
Output: [3.6799999999999997, -0.9999999999999998]
Hints
Hint 1
There are N³ equally likely outcomes in total. How many ways can all three dice land on the same face? How does that fraction simplify?
Hint 2
For the pair case, think about it in two steps: choose which value is repeated (N choices), then choose which of the three dice is the odd one out (3 choices), then choose a different value for that die (N−1 choices).
Hint 3
Once you have the three probabilities, double-check they sum to 1 before computing expected values. The only difference between the two versions is the sign in front of the all-different term.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Counting the three outcome classes
There are N³ equally likely ordered outcomes (die 1, die 2, die 3).
Triple — all three dice agree. There are exactly N such outcomes (one per face), giving probability N / N³ = 1 / N².
Pair — exactly two dice agree. Pick the repeated value: N ways. Pick which of the three dice is the odd one out: 3 ways. Pick a different value for that lone die: N−1 ways. Total: 3·N·(N−1) outcomes, probability 3(N−1) / N².
All different — no two dice share a value. Ordered choices with no repeats: N·(N−1)·(N−2) outcomes, probability (N−1)(N−2) / N².
Sanity check — the three probabilities sum to:
[1 + 3(N−1) + (N−1)(N−2)] / N²
= [1 + 3N − 3 + N² − 3N + 2] / N²
= N² / N² = 1 ✓
Expected value
Multiply each probability by its payoff and sum. The only difference between the two versions is the sign on the all-different term:
- Version 1:
E = A·p_triple + B·p_pair + C·p_all_diff - Version 2:
E = A·p_triple + B·p_pair − C·p_all_diff
Edge cases worth noting
- N = 1: only a triple is possible (probability 1). Both versions return A, which the formula handles correctly since (N−1) = 0 zeroes out the other terms.
- N = 2: no all-different outcome is possible (N−2 = 0), so both versions return the same value — confirmed by the second sample case.
Complexity
O(1) time and space — just arithmetic on the four inputs.
Reference solution
from typing import List
def dice_match_expected_payoff(N: int, A: float, B: float, C: float) -> List[float]:
n_squared = N * N
p_triple = 1.0 / n_squared
p_pair = 3.0 * (N - 1) / n_squared
p_all_diff = (N - 1) * (N - 2) / n_squared
version_1 = A * p_triple + B * p_pair + C * p_all_diff
version_2 = A * p_triple + B * p_pair - C * p_all_diff
return [version_1, version_2]