Data Structures & AlgorithmsModerateTCS NQT · bit operations

Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code.

Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code.
from itertools import combinations
print(len({x ^ y for x, y in combinations([1, 2, 3], 2)}))
What is printed?
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 6

Correct answer: A. 3

The three pairs give XOR values 3, 2 and 1, all distinct. Counting pairs instead of distinct values gives 3 as well, so the set form is what is being tested here.

More Coding Interview Problems questions

Exams that ask this topic