Data Structures & AlgorithmsHardTCS NQT · probability counting

Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code.

Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code.
print(sum(1 for a in range(1, 7) for b in range(1, 7) for c in range(1, 7) if a + b + c == 9))
What is printed?
  1. A. 21
  2. B. 27
  3. C. 30
  4. D. 25

Correct answer: D. 25

The number of ordered triples summing to 9 is 25. Counting unordered multisets instead gives only 6, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic