KatInterview

Calculate Expected Roll Counts for Target Dice Sequences

Arrays & HashingAuto-graded · 17 test cases
HardAuto-gradedArrays & Hashingprobabilityexpected-valuemarkov-chains

Last 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:

  1. Part 1: The expected total number of rolls to obtain the first occurrence of a $6$.
  2. 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.