Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 200] are divisible by 2, 3 or 5?

Problem: How many integers in [1, 200] are divisible by 2, 3 or 5?
print(sum(1 for x in range(1, 201) if x % 2 == 0 or x % 3 == 0 or x % 5 == 0))
What is printed?
  1. A. 206
  2. B. 146
  3. C. 140
  4. D. 145

Correct answer: B. 146

Inclusion–exclusion: 100 + 66 + 40 − (33 + 20 + 13) + 6 = 146. Just adding the three counts gives 206, subtracting the pairwise overlaps but forgetting to add back the 6 multiples of 30 gives 140, and range(1, 200) misses 200 itself (145).

More Coding Interview Problems questions

Exams that ask this topic