Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code.

Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code.
from itertools import combinations
print(sum(1 for c in combinations(range(1, 11), 3) if sum(c) == 15))
What is printed?
  1. A. 60
  2. B. 15
  3. C. 10
  4. D. 120

Correct answer: C. 10

Triples with sum 15 from 1..10 are (1,4,10), (1,5,9), (1,6,8), (2,3,10), (2,4,9), (2,5,8), (2,6,7), (3,4,8), (3,5,7), (4,5,6): 10. Ordered triples give 60, allowing repeats gives 15, and 120 is all 3-subsets.

More Coding Interview Problems questions

Exams that ask this topic