Data Structures & AlgorithmsModerateTCS NQT · bit operations

Problem: How many unordered pairs from {1, 2, 3, 4, 5, 6} have XOR equal to 7? Run the code.

Problem: How many unordered pairs from {1, 2, 3, 4, 5, 6} have XOR equal to 7? Run the code.
from itertools import combinations
print(sum(1 for a, b in combinations([1, 2, 3, 4, 5, 6], 2) if a ^ b == 7))
What is printed?
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 1

Correct answer: C. 3

1 XOR 6 = 7, 2 XOR 5 = 7 and 3 XOR 4 = 7, and no other pair of these values gives 7.

More Coding Interview Problems questions

Exams that ask this topic