Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many distinct values does x mod 12 take over the numbers x in [1, 1000] that are not divisible by 4? Run the code.

Problem: How many distinct values does x mod 12 take over the numbers x in [1, 1000] that are not divisible by 4? Run the code.
print(len({x % 12 for x in range(1, 1001) if x % 4 != 0}))
What is printed?
  1. A. 9
  2. B. 6
  3. C. 12
  4. D. 3

Correct answer: A. 9

x not divisible by 4 means x mod 12 avoids 0, 4, 8, leaving 9 residues, and all appear by 1000. Also removing multiples of 3 gives 6, 12 counts every residue, and 3 counts the excluded ones.

More Coding Interview Problems questions

Exams that ask this topic