Generate all outcomes of rolling n distinguishable m-sided dice recursively

Quick Overview

Generate every outcome of rolling n distinguishable dice with m faces each, returned as lists of face values in lexicographic order and built recursively. It tests backtracking, copying the partial result at the base case, and counting the m to the power n outcomes.

Generate all outcomes of rolling n distinguishable m-sided dice recursively

Company: Crowdstrike

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two integers `n` and `m`, generate all possible outcomes of rolling `n` dice, where each die has `m` faces numbered `1` through `m`. Each outcome is a list of `n` values, where the `i`-th value is the face shown by the `i`-th die. Dice are distinguishable, so `[1, 2]` and `[2, 1]` are different outcomes. Implement the solution recursively. ### Function Signature ```python def dice_rolls(n: int, m: int) -> list[list[int]]: ``` ### Rules - Return every outcome exactly once, so the result has `m ** n` lists. - Return the outcomes in ascending lexicographic order: compare the first die's face, then the second die's face on a tie, and so on. ### Constraints - `1 <= n <= 5` - `1 <= m <= 6` ### Examples ```text Input: n = 2, m = 2 Output: [[1,1],[1,2],[2,1],[2,2]] ``` Each of the 2 dice can show 1 or 2, giving 2 * 2 = 4 outcomes. `[1,2]` and `[2,1]` both appear because the dice are distinguishable. ```text Input: n = 1, m = 3 Output: [[1],[2],[3]] ``` A single die with 3 faces has 3 outcomes. ```text Input: n = 3, m = 2 Output: [[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,1],[2,1,2],[2,2,1],[2,2,2]] ``` Three dice with 2 faces each give 2 * 2 * 2 = 8 outcomes. All outcomes that start with 1 come before those that start with 2, and the same rule orders the second and third positions.

Overview: Generate every outcome of rolling n distinguishable dice with m faces each, returned as lists of face values in lexicographic order and built recursively. It tests backtracking, copying the partial result at the base case, and counting the m to the power n outcomes.

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

Given two integers n and m, generate all possible outcomes of rolling n dice, where each die has m faces numbered 1 through m.

Each outcome is a list of n values, where the i-th value is the face shown by the i-th die. Dice are distinguishable, so [1, 2] and [2, 1] are different outcomes.

Implement the solution recursively.

Function Signature

def dice_rolls(n: int, m: int) -> list[list[int]]:

Rules

  • Return every outcome exactly once, so the result has m ** n lists.
  • Return the outcomes in ascending lexicographic order: compare the first die's face, then the second die's face on a tie, and so on.

Constraints

  • 1 <= n <= 5
  • 1 <= m <= 6

Examples

Input: n = 2, m = 2
Output: [[1,1],[1,2],[2,1],[2,2]]

Each of the 2 dice can show 1 or 2, giving 2 * 2 = 4 outcomes. [1,2] and [2,1] both appear because the dice are distinguishable.

Input: n = 1, m = 3
Output: [[1],[2],[3]]

A single die with 3 faces has 3 outcomes.

Input: n = 3, m = 2
Output: [[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,1],[2,1,2],[2,2,1],[2,2,2]]

Three dice with 2 faces each give 2 * 2 * 2 = 8 outcomes. All outcomes that start with 1 come before those that start with 2, and the same rule orders the second and third positions.