Data Structures & AlgorithmsModerateTCS NQT · counting

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

Problem: How many integers in [1, 100] are divisible by 7 or by 11? Run the code.
print(sum(1 for x in range(1, 101) if x % 7 == 0 or x % 11 == 0))
What is printed?
  1. A. 23
  2. B. 22
  3. C. 21
  4. D. 1

Correct answer: B. 22

Inclusion–exclusion: 14 multiples of 7 + 9 multiples of 11 − 1 multiple of 77 = 22. Forgetting to subtract 77 gives 23, subtracting it twice gives 21, and testing 'and' instead of 'or' counts only 77 (1).

More Coding Interview Problems questions

Exams that ask this topic