Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code.

Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code.
print(sum(1 for x in range(1, 1001) if x % 2 == 0 and x % 5 == 0 and x % 4 != 0))
What is printed?
  1. A. 50
  2. B. 100
  3. C. 75
  4. D. 150

Correct answer: A. 50

Divisible by 2 and 5 means a multiple of 10 (100 of them); removing the 50 multiples of 20 leaves 50. 100 ignores the 'not by 4' part, subtracting multiples of 40 gives 75, and dropping the 'by 2' condition gives 200 − 50 = 150.

More Coding Interview Problems questions

Exams that ask this topic