Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 10000] have a digit sum that is divisible by 7? Run the code.

Problem: How many integers in [1, 10000] have a digit sum that is divisible by 7? Run the code.
print(sum(1 for x in range(1, 10001) if sum(map(int, str(x))) % 7 == 0))
What is printed?
  1. A. 1428
  2. B. 120
  3. C. 1421
  4. D. 139

Correct answer: C. 1421

Digit sums 7, 14, 21, 28, 35 in 1..10000 give 1421 numbers. 1428 counts multiples of 7 (digit sum ≠ value mod 7), digit sum exactly 7 gives 120, and stopping at 1000 gives 139.

More Coding Interview Problems questions

Exams that ask this topic