FastPrepUnique Unordered Dice Outcomes

Unique Unordered Dice Outcomes

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A die has faces labeled from 1 through faces and is rolled rolls times. Return every distinct outcome when roll order does not matter.

Represent each outcome as a nondecreasing array of face values. Return all outcomes in lexicographic order. Repeated face values are allowed. When rolls = 0, return one empty outcome.

Function

unorderedDiceOutcomes(faces: int, rolls: int) → int[][]

Examples

Example 1

faces = 3rolls = 2return = [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]

Each pair is nondecreasing, so order-equivalent results such as [1,2] and [2,1] appear only once.

Example 2

faces = 2rolls = 0return = [[]]

There is exactly one way to make zero rolls: the empty outcome.

Constraints

  • 1 <= faces <= 15
  • 0 <= rolls <= 10
  • The number of returned outcomes is at most 100000

More Microsoft problems

See Microsoft hiring insights
public int[][] unorderedDiceOutcomes(int faces, int rolls) {
  // Write your code here.
}
faces3
rolls2
expected[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
Checking account…