Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the sum of all integers below 1000 that are multiples of 3 or 5? Run the code.

Problem: What is the sum of all integers below 1000 that are multiples of 3 or 5? Run the code.
print(sum(x for x in range(1000) if x % 3 == 0 or x % 5 == 0))
What is printed?
  1. A. 233168
  2. B. 266333
  3. C. 234168
  4. D. 200003

Correct answer: A. 233168

Inclusion-exclusion: 166833 + 99500 − 33165 = 233168. Not removing multiples of 15 gives 266333, including 1000 gives 234168, and subtracting them twice gives 200003.

More Coding Interview Problems questions

Exams that ask this topic