Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers x in [1, 999] satisfy x ≡ 2 (mod 3) and x ≡ 5 (mod 7)? Run the code.

Problem: How many integers x in [1, 999] satisfy x ≡ 2 (mod 3) and x ≡ 5 (mod 7)? Run the code.
print(sum(1 for x in range(1, 1000) if x % 3 == 2 and x % 7 == 5))
What is printed?
  1. A. 47
  2. B. 48
  3. C. 333
  4. D. 143

Correct answer: B. 48

By CRT the solutions are x ≡ 5 (mod 21): 5, 26, …, 992, which is 48 values. 999 // 21 ignores the offset and gives 47, x ≡ 2 (mod 3) alone gives 333, and x ≡ 5 (mod 7) alone gives 143.

More Coding Interview Problems questions

Exams that ask this topic