Data Structures & AlgorithmsModerateCAT · TCS NQT

Problem: Count integers from 1 to 100 that are divisible by 3 or by 5 but not by 15.

  1. A. 47
  2. B. 53
  3. C. 35
  4. D. 41

Correct answer: D. 41

33 multiples of 3 plus 20 of 5 count the 6 multiples of 15 twice, and those must be excluded entirely: 33 + 20 − 2·6 = 41. Plain inclusion–exclusion (3 or 5) gives 47, forgetting the overlap gives 53, and subtracting the 15s three times gives 35.

More Coding Interview Problems questions

Exams that ask this topic