Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code.

Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code.
print(sum(1 for a in range(1, 8) for b in range(1, 8) for c in range(1, 8) if a + b + c == 7))
What is printed?
  1. A. 36
  2. B. 15
  3. C. 4
  4. D. 21

Correct answer: B. 15

Positive solutions of a + b + c = 7: C(6,2) = 15. Allowing zeros gives C(9,2) = 36, unordered triples give 4, and C(7,2) = 21 uses the wrong stars-and-bars formula.

More Coding Interview Problems questions

Exams that ask this topic