Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code.

Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code.
print(sum(1 for x in range(1, 201) if (x * x + 2 * x + 1) % 7 == 0))
What is printed?
  1. A. 56
  2. B. 0
  3. C. 28
  4. D. 4

Correct answer: C. 28

(x + 1)² ≡ 0 (mod 7) iff 7 | x + 1 because 7 is prime, so x = 6, 13, …, 195: 28 values. Expecting two roots per period gives 56, testing x² + 1 gives 0, and wrongly requiring 49 | x + 1 gives 4.

More Coding Interview Problems questions

Exams that ask this topic