Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 1000] are divisible by 7, 11 or 13? Run the code.

Problem: How many integers in [1, 1000] are divisible by 7, 11 or 13? Run the code.
print(sum(1 for x in range(1, 1001) if x % 7 == 0 or x % 11 == 0 or x % 13 == 0))
What is printed?
  1. A. 308
  2. B. 280
  3. C. 252
  4. D. 220

Correct answer: B. 280

Inclusion-exclusion: 142 + 90 + 76 − (14 + 10 + 6) + 0 = 280. Skipping the overlaps gives 308, subtracting them twice gives 252, and forgetting 13 entirely gives 220.

More Coding Interview Problems questions

Exams that ask this topic