Data Structures & AlgorithmsModerateTCS NQT · number puzzle

Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code.

Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code.
print(sum(1 for x in range(1, 101) if sum(map(int, str(x))) == 10))
What is printed?
  1. A. 12
  2. B. 10
  3. C. 9
  4. D. 45

Correct answer: C. 9

The values are 19, 28, 37, 46, 55, 64, 73, 82 and 91; 100 has digit sum 1. Using the mod-9 shortcut x % 9 == 1 also catches 1, 10 and 100 (12), counting digit sum 9 gives 10, and testing ≥ 10 instead of == 10 gives 45.

More Coding Interview Problems questions

Exams that ask this topic