Magicsheet logo

Maximum XOR of Two Numbers in an Array

Medium
30.2%
Updated 6/1/2025

Maximum XOR of Two Numbers in an Array

What is this problem about?

The "Maximum XOR of Two Numbers in an Array" coding problem asks you to find the maximum possible result of nums[i] ^ nums[j] where i != j and nums is an array of non-negative integers. This problem is a classic example of using bit manipulation and specialized data structures to optimize a seemingly quadratic problem. A naive approach would involve checking every pair, resulting in O(N^2) time complexity. However, the problem can be solved much more efficiently by leveraging the properties of bitwise XOR to build an optimal matching strategy, often involving a Trie (Prefix Tree) or clever hash set usage.

Why is this asked in interviews?

This Maximum XOR of Two Numbers in an Array interview question is a favorite for companies like Google, Microsoft, and Amazon because it effectively assesses a candidate's understanding of bitwise operations and their application in conjunction with advanced data structures. It evaluates your ability to think about numbers in their binary representation and how to strategically build the maximum possible XOR sum bit by bit. The problem specifically targets the efficient searching for a complementary number that maximizes the XOR sum. It's a robust problem that highlights your analytical skills and grasp of array, bit manipulation, and Trie interview patterns, demonstrating your ability to optimize for performance.

Algorithmic pattern used

The most efficient algorithmic pattern for solving the "Maximum XOR of Two Numbers in an Array" problem is to use a Trie (Prefix Tree) for Bit Manipulation.

  1. Build Trie: Insert all numbers from the nums array into a binary Trie. Each node in the Trie represents a bit (0 or 1). For each number, traverse from the most significant bit (MSB) down to the least significant bit (LSB), adding nodes to the Trie as needed.
  2. Query for Maximum XOR: For each number num in nums:
    • Traverse the Trie, starting from the MSB. At each bit position b:
      • If the b-th bit of num is 0: Try to go down the 1 child path in the Trie. If it exists, this contributes 1 << b to the current XOR sum, and you continue down that path. Otherwise, go down the 0 child path.
      • If the b-th bit of num is 1: Try to go down the 0 child path in the Trie. If it exists, this contributes 1 << b to the current XOR sum, and you continue down that path. Otherwise, go down the 1 child path.
    • The maximum XOR sum found during this traversal (for the current num) is a candidate for the overall maximum.
    • Update the overall max_xor_sum. This array, bit manipulation, and Trie interview pattern provides an efficient O(N * MaxBits) solution, where MaxBits is the number of bits required to represent the largest number in nums.

Example explanation

Consider nums = [3, 10, 5, 25, 2, 8]. Max 25 needs 5 bits (11001_2). Build Trie: Insert: 3 (00011), 10 (01010), 5 (00101), 25 (11001), 2 (00010), 8 (01000).

Query for max XOR: Take num = 25 (11001_2)

  • MSB (bit 4): 25 has 1. Try to find 0 in Trie. Yes, path exists. Current XOR = 10000_2.
  • Bit 3: 25 has 1. Try to find 0 in Trie. Yes, path exists. Current XOR = 11000_2.
  • Bit 2: 25 has 0. Try to find 1 in Trie. Yes, path exists. Current XOR = 11100_2.
  • Bit 1: 25 has 0. Try to find 1 in Trie. Yes, path exists. Current XOR = 11110_2.
  • Bit 0: 25 has 1. Try to find 0 in Trie. Yes, path exists. Current XOR = 11111_2. The number found in Trie that maximizes XOR with 25 is 6. And 25 ^ 6 = 27. (This example is simplified for illustration, actual Trie path construction is more detailed).

A more direct example: nums = [3, 8, 10]. Max 10 needs 4 bits. Binary: 3 (0011), 8 (1000), 10 (1010)

Build Trie (simplified visualization): Root 0 (for 3) 0 1 1 1 (for 8, 10) 0 (for 8, 10) 0 (for 8) 0 (for 8) 1 (for 10) 0 (for 10)

Query for num = 3 (0011): Goal: Maximize 3 ^ X. Try to find 1100 pattern.

  • Bit 3 (MSB): 3 has 0. Try path 1. Yes. current_xor_val = 1000_2 = 8.
  • Bit 2: 3 has 0. Try path 1. No. Go path 0. current_xor_val remains 8.
  • Bit 1: 3 has 1. Try path 0. Yes. current_xor_val = 8 | 0010_2 = 10.
  • Bit 0: 3 has 1. Try path 0. Yes. current_xor_val = 10 | 0000_2 = 10. The number 8 XORed with 3 gives 11. So 3 ^ 8 = 11. If we query for num = 10 (1010): Goal: Maximize 10 ^ X. Try to find 0101 pattern.
  • Bit 3 (MSB): 10 has 1. Try path 0. Yes. current_xor_val = 0000_2 = 0.
  • Bit 2: 10 has 0. Try path 1. Yes. current_xor_val = 0100_2 = 4.
  • Bit 1: 10 has 1. Try path 0. Yes. current_xor_val = 0101_2 = 5.
  • Bit 0: 10 has 0. Try path 1. Yes. current_xor_val = 0101_2 = 5. The number 5 XORed with 10 gives 15. So 10 ^ 5 = 15.

Max XOR is 15. This demonstrates the Trie's power for the Maximum XOR of Two Numbers in an Array coding problem.

Common mistakes candidates make

A common mistake in the Maximum XOR of Two Numbers in an Array coding problem is attempting a brute-force O(N^2) solution, which is too slow for large inputs. Another frequent error is incorrectly implementing the binary Trie, especially when inserting numbers or performing the maximum XOR search (e.g., not traversing from MSB to LSB, or incorrect handling of missing paths). Some might struggle with the bit manipulation logic when constructing the XOR sum bit by bit. Overlooking the possibility of having to insert numbers into the Trie from highest possible bit down to lowest (e.g., padding with leading zeros) can also lead to issues.

Interview preparation tip

To ace the Maximum XOR of Two Numbers in an Array interview question, gain a deep understanding of Tries (Prefix Trees) and their application to bitwise problems. Practice implementing Trie insertion and search operations. Focus on how to leverage the MSB to LSB traversal in a Trie to greedily build the maximum XOR sum. Understand the properties of XOR and how it can be used to maximize a value. This array, bit manipulation, and Trie interview pattern is a challenging but common problem, and mastering it showcases advanced algorithmic skills.

Similar Questions