Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 300] are divisible by exactly two of the numbers 2, 3 and 5? Run the code.

Problem: How many integers in [1, 300] are divisible by exactly two of the numbers 2, 3 and 5? Run the code.
print(sum(1 for x in range(1, 301) if sum(1 for d in (2, 3, 5) if x % d == 0) == 2))
What is printed?
  1. A. 80
  2. B. 100
  3. C. 90
  4. D. 70

Correct answer: D. 70

Pairs: 6 → 50, 10 → 30, 15 → 20, minus 3 × 10 multiples of 30 that fall in all three, so 70. 'At least two' gives 80, the raw pairwise sum is 100, and subtracting the triple once gives 90.

More Coding Interview Problems questions

Exams that ask this topic