Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code.

Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code.
print(sum(x for x in range(1, 200) if x % 3 != 0 and sum(1 for y in range(1, x + 1) if y % 3 != 0) <= 100))
What is printed?
  1. A. 3367
  2. B. 5050
  3. C. 11325
  4. D. 7500

Correct answer: D. 7500

The first 100 non-multiples of 3 are 1..149 minus 3, 6, …, 147: 11175 − 3675 = 7500. Stopping at 100 gives 3367, 5050 sums 1..100, and 11325 sums 1..150 without removing anything.

More Coding Interview Problems questions

Exams that ask this topic