Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code.

Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code.
a = [3, 10, 5, 25, 2, 8, 17]
print(max(a[i] ^ a[j] for i in range(len(a)) for j in range(i + 1, len(a))))
What is printed?
  1. A. 29
  2. B. 28
  3. C. 42
  4. D. 25

Correct answer: B. 28

The best pair is 25 ⊕ 5 = 28 (11001 ⊕ 00101 = 11100). Using OR gives 25 | 5 = 29, using + gives 42, and 25 assumes the maximum element alone wins.

More Coding Interview Problems questions

Exams that ask this topic