Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many distinct values does (i j) mod 7 take for 1 <= i, j <= 7? Run the code.

Problem: How many distinct values does (i * j) mod 7 take for 1 <= i, j <= 7? Run the code.
print(len({(i * j) % 7 for i in range(1, 8) for j in range(1, 8)}))
What is printed?
  1. A. 6
  2. B. 49
  3. C. 28
  4. D. 7

Correct answer: D. 7

Since 7 is prime, products of 1..6 give all 6 non-zero residues, and j = 7 adds 0, so 7 values. Forgetting that 7 ≡ 0 gives 6, 49 counts pairs, and 28 counts unordered pairs.

More Coding Interview Problems questions

Exams that ask this topic