Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A dice-throwing game: starting at 0, each throw adds a value from 1 to 6, and the player stops as soon as the total is at least 20. How many throw sequences (ordered) end exactly at a total of 20?

  1. A. 233904
  2. B. 920319
  3. C. 463968
  4. D. 524288

Correct answer: C. 463968

Every partial sum before the last throw is below 20, so the sequences are exactly the compositions of 20 into parts 1-6: f(n) = f(n − 1) + … + f(n − 6) gives 463968. An off-by-one total gives 233904 (19) or 920319 (21), and allowing parts of any size gives 2¹⁹ = 524288.

More Coding Interview Problems questions

Exams that ask this topic