Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code.

Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code.
print(sum(1 for x in range(1, 301) if x % 6 == 0 and x % 9 != 0))
What is printed?
  1. A. 34
  2. B. 45
  3. C. 50
  4. D. 17

Correct answer: A. 34

Multiples of 6 (50) minus multiples of lcm(6, 9) = 18 (16) gives 34. Using 6 × 9 = 54 gives 45, 50 ignores the condition, and subtracting multiples of 9 gives 17.

More Coding Interview Problems questions

Exams that ask this topic