Data Structures & AlgorithmsHardTCS NQT · counting

Problem: How many triples (a, b, c) with 1 <= a <= b <= c <= 10 have a + b + c = 12? Run the code.

Problem: How many triples (a, b, c) with 1 <= a <= b <= c <= 10 have a + b + c = 12? Run the code.
print(sum(1 for a in range(1, 11) for b in range(a, 11) for c in range(b, 11) if a + b + c == 12))
What is printed?
  1. A. 10
  2. B. 14
  3. C. 12
  4. D. 9

Correct answer: C. 12

Counting non-decreasing triples with sum 12 gives 12. Including ordered triples (all permutations) would give a much larger number, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic