Number of Dice Rolls With Target Sum

MediumDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You roll n dice, each with faces numbered 1 through k. The dice are distinguishable, so the ordered list of outcomes matters (rolling 1 then 2 differs from 2 then 1). Return the number of outcome lists whose faces sum to exactly target, modulo 10^9 + 7.

Examples

Input: n = 1, k = 6, target = 3
Output: 1
Only rolling a 3.
Input: n = 2, k = 6, target = 7
Output: 6
1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
Input: n = 30, k = 30, target = 500
Output: 222616187
The count modulo 10^9 + 7.

Constraints

  • 1 <= n, k <= 30
  • 1 <= target <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems