Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code.

Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code.
print(sum(1 for x in range(1, 1001) if sum(map(int, str(x))) == 5 and x % 5 == 0))
What is printed?
  1. A. 21
  2. B. 6
  3. C. 7
  4. D. 1

Correct answer: C. 7

Digit sum 5 and ending in 0 or 5: 5, 50, 140, 230, 320, 410, 500, so 7. Ignoring divisibility gives 21, allowing only a final 0 gives 6, and allowing only a final 5 gives 1.

More Coding Interview Problems questions

Exams that ask this topic