Count Ways K Dice Rolls Land on an Exact Target Score
Last updated: November 29, 2026
Quick Overview
A board game in Google Play Games rolls n dice each with f faces numbered 1 to f. Count the outcomes (ordered) whose total is exactly T, modulo 1,000,000,007.
0
0
0 solved
A board game in Google Play Games rolls n dice each with f faces numbered 1 to f. Count the outcomes (ordered) whose total is exactly T, modulo 1,000,000,007.
An online-assessment style counting problem where the natural table is 2D but only the previous row is needed. Graders look for a rolling 1D array and for using a sliding window sum to avoid the f factor.
What the Interviewer Expects
- Define ways[s] as outcomes after the current number of dice
- Roll the table one die at a time with a fresh array
- Use a prefix or sliding window sum to make each step O(T)
- Apply the modulus
- Prune impossible targets early
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Each die has its own number of faces.
- Count outcomes where no two consecutive dice show the same value.
- Compute the probability instead of a count, with n up to 1000.
- Return the most likely total.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: integers n >= 1 dice, f >= 1 faces, target T. Output the number of sequences (d1, ..., dn) with each di in 1..f summing to T, mod . If T < n or T > n * f, the answer is 0....
High-Level Design
After rolling i dice, ways[s] is the number of sequences summing to s. Adding one die: new[s] = ways[s-1] + ways[s-2] + ... + ways[s-f]. That is a window sum of width f over the previous array, so...