Count the ways to roll n k-sided dice to a target sum

Quick Overview

Count the ordered ways to roll n dice, each with faces 1 to k, so that the face values add up to a target, with the count returned modulo 1,000,000,007. It tests dynamic programming over the number of dice and the running sum, plus careful handling of very large counts.

Count the ways to roll n k-sided dice to a target sum

Company: Crowdstrike

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You have `n` dice, and each die has `k` faces numbered from `1` to `k`. Given three integers `n`, `k`, and `target`, return the number of possible ways (out of the $k^n$ total ways) to roll the dice so the sum of the face-up numbers equals `target`. Since the answer may be very large, return it modulo ${10^9 + 7}$. ### Function Signature ```python def num_rolls_to_target(n: int, k: int, target: int) -> int: ... ``` ### Rules - The dice are distinguishable, so a roll is an ordered sequence of `n` face values. Rolling `(1, 6)` and rolling `(6, 1)` count as two different ways. - Return the count modulo `1000000007`. When no roll reaches `target`, return `0`. ### Constraints - `1 <= n, k <= 30` - `1 <= target <= 1000` ### Examples ```text Input: n = 1, k = 6, target = 3 Output: 1 ``` With one six-sided die, the only way to get a sum of 3 is to roll a 3. ```text Input: n = 2, k = 6, target = 7 Output: 6 ``` The six ordered rolls are (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1). ```text Input: n = 2, k = 6, target = 13 Output: 0 ``` Two six-sided dice sum to at most 12, so no roll reaches 13.

Overview: Count the ordered ways to roll n dice, each with faces 1 to k, so that the face values add up to a target, with the count returned modulo 1,000,000,007. It tests dynamic programming over the number of dice and the running sum, plus careful handling of very large counts.

Crowdstrike logo
Crowdstrike
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You have n dice, and each die has k faces numbered from 1 to k.

Given three integers n, k, and target, return the number of possible ways (out of the knk^n total ways) to roll the dice so the sum of the face-up numbers equals target. Since the answer may be very large, return it modulo 109+7{10^9 + 7}.

Function Signature

def num_rolls_to_target(n: int, k: int, target: int) -> int:
    ...

Rules

  • The dice are distinguishable, so a roll is an ordered sequence of n face values. Rolling (1, 6) and rolling (6, 1) count as two different ways.
  • Return the count modulo 1000000007 . When no roll reaches target , return 0 .

Constraints

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

Examples

Input: n = 1, k = 6, target = 3
Output: 1

With one six-sided die, the only way to get a sum of 3 is to roll a 3.

Input: n = 2, k = 6, target = 7
Output: 6

The six ordered rolls are (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1).

Input: n = 2, k = 6, target = 13
Output: 0

Two six-sided dice sum to at most 12, so no roll reaches 13.