The Dice Roll Simulation interview question asks you to find the number of distinct sequences of dice rolls such that no number appears more than rollMax[i] times consecutively. For example, if rollMax[1] = 2, you cannot have [1, 1, 1] in your sequence. Return the answer modulo .
This "Hard" problem is a favorite at Akuna Capital because it tests your mastery of Dynamic Programming. It evaluations whether you can define a state that captures all necessary constraints—in this case, not just the number of rolls, but also what the last roll was and how many times it has repeated. It’s a test of "State Expansion."
This is a 3D Dynamic Programming problem.
State: dp[i][last_val][count]
i: Number of rolls completed.last_val: The value of the last die roll (1-6).count: How many times last_val has appeared consecutively so far.
Transition:count + 1 <= rollMax[v], move to dp[i+1][v][count+1].dp[i+1][v][1]., rollMax = [1, 1, 2, 2, 2, 2].
rollMax[1]=1).rollMax[3]=2).
Total valid sequences are summed up.count is bounded by the maximum value in rollMax (usually small, like 15), keeping the DP table manageable.When a DP problem has a "consecutive" constraint, your state must include "how many of the current type I just placed." This is a standard template for sequence-based DP.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Arithmetic Slices II - Subsequence | Hard | Solve | |
| Best Time to Buy and Sell Stock III | Hard | Solve | |
| Best Time to Buy and Sell Stock IV | Hard | Solve | |
| Burst Balloons | Hard | Solve | |
| Choose Numbers From Two Arrays in Range | Hard | Solve |