Calculate Expected Roll Counts for Target Dice Sequences
Arrays & HashingAuto-graded · 17 test casesLast updated
Evaluate expected values for geometric process waiting times and pattern completion using Markov chain state transitions.
Calculate the expected number of rolls needed when rolling a fair, six-sided die to hit two specific stopping conditions:
- Part 1: The expected total number of rolls to obtain the first occurrence of a $6$.
- Part 2: The expected total number of rolls to observe the sequence $5$ followed immediately by a $6$ (e.g., in the roll sequence
[2, 4, 5, 6], the game stops on roll 4).
Both expected values should be returned as a list of floats [expected_rolls_part_1, expected_rolls_part_2].
Constraints
- The die is fair (probability of rolling any side $1$ through $6$ is exactly $1/6$).
- Each die roll is independent and identically distributed.
- In Part 2, consecutive $5$s retain progress towards the pattern. For instance, in
[5, 5, 6], the target sequence ends on the 3rd roll.
Examples
Example 1
Input: expected_dice_rolls()
Output: [6, 36]
Example 2
Input: expected_dice_rolls()
Output: [6, 36]
Hints
Hint 1
For Part 1, model the process as independent Bernoulli trials where rolling a 6 is a success with probability p = 1/6.
Hint 2
For Part 2, construct a Markov chain with states tracking the length of the matching pattern prefix (0 matching rolls, or 1 matching roll of '5').
Hint 3
Pay attention to the transition from the '5' state when rolling another '5'—it keeps the process in the '5' state rather than resetting to the initial state.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1: Rolling a single target value
The process consists of independent rolls where each trial succeeds with probability $p = \frac{1}{6}$. The total number of rolls $N$ follows a Geometric distribution with parameter $p$. By first-step analysis:
$$E = 1 + \frac{5}{6} E \implies \frac{1}{6} E = 1 \implies E = 6$$
Part 2: Rolling a consecutive sequence (5 followed by 6)
We define the state space based on the prefix of the sequence [5, 6] matched so far:
- State $S_0$: The last roll was not $5$ (0 matched characters).
- State $S_1$: The last roll was $5$ (1 matched character).
- State $S_2$: The target
[5, 6]sequence is achieved (absorbing state).
Let $E_0$ and $E_1$ represent the expected remaining rolls needed from $S_0$ and $S_1$ respectively.
From $S_0$, we make one roll:
- Roll a $5$ (probability $\frac{1}{6}$): transition to $S_1$.
- Roll any other number (probability $\frac{5}{6}$): stay in $S_0$.
$$E_0 = 1 + \frac{5}{6} E_0 + \frac{1}{6} E_1 \implies E_0 = 6 + E_1$$
From $S_1$, we make one roll:
- Roll a $6$ (probability $\frac{1}{6}$): sequence completed ($0$ additional expected rolls).
- Roll a $5$ (probability $\frac{1}{6}$): stay in $S_1$ because the new $5$ serves as a valid starting prefix.
- Roll $1, 2, 3,$ or $4$ (probability $\frac{4}{6}$): transition back to $S_0$.
$$E_1 = 1 + \frac{1}{6}(0) + \frac{1}{6} E_1 + \frac{4}{6} E_0$$
Substituting $E_0 = 6 + E_1$ into the second equation:
$$\frac{5}{6} E_1 = 1 + \frac{4}{6}(6 + E_1) = 5 + \frac{4}{6} E_1 \implies \frac{1}{6} E_1 = 5 \implies E_1 = 30$$
Hence, $E_0 = 6 + 30 = 36$.
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$ since the expected values are constants computed analytically.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space.
from typing import List
def expected_dice_rolls() -> List[float]:
# Part 1: Geometric distribution with p = 1/6 -> E = 1 / (1/6) = 6.0
expected_first_six = 6.0
# Part 2: Markov chain first-step analysis -> E0 = 36.0
expected_five_then_six = 36.0
return [expected_first_six, expected_five_then_six]