Data Structures & AlgorithmsModerateCAT · TCS NQT

Problem: In a list of the first 30 positive integers, how many numbers are multiples of 2 or 3 but not of 6?

  1. A. 20
  2. B. 15
  3. C. 25
  4. D. 10

Correct answer: B. 15

Multiples of 2 or 3 in 1..30: 15 + 10 − 5 = 20; removing the 5 multiples of 6 leaves 15. Stopping at 20 forgets the exclusion, 15 + 10 = 25 skips inclusion-exclusion, and 10 subtracts the 6s twice.

More Coding Interview Problems questions

Exams that ask this topic