Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code.

Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code.
print(len({a ^ b for a in range(4) for b in range(4)}))
What is printed?
  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: A. 4

XOR over the 2-bit values is a group, so all four values 0..3 appear.

More Coding Interview Problems questions

Exams that ask this topic