Maximum XOR of Two Numbers in an Array

Algorithms
Medium
Google
22.4K views

Given an array of integers, find the maximum result of $a \text{ XOR } b$, where $a$ and $b$ are elements in the array. Use a Trie (Prefix Tree) optimized for Bit Manipulation.

Why Interviewers Ask This

Google interviewers ask this to evaluate a candidate's mastery of bit manipulation and their ability to optimize brute-force solutions. They specifically test if you can recognize that comparing every pair is inefficient, requiring a shift from O(N^2) to O(32N) using a Trie. This assesses your capacity to think in binary representations and design data structures that exploit bitwise properties for performance.

How to Answer This Question

1. Clarify Constraints: Immediately confirm the integer range (e.g., 32-bit signed) and array size to determine if an O(N^2) solution passes or if optimization is mandatory. 2. Propose Brute Force: Briefly outline the naive approach of checking all pairs to establish a baseline complexity of O(N^2), then explicitly state why this fails for large inputs. 3. Introduce the Trie Concept: Explain how inserting numbers into a binary Trie allows us to greedily construct the maximum XOR by traversing bits from most significant to least significant. 4. Detail the Greedy Strategy: Describe the logic of trying to move to the opposite bit (0 vs 1) at each level; if the opposite path exists, take it to maximize the result, otherwise follow the available path. 5. Walk Through Complexity: Conclude by analyzing time complexity as O(32 * N) and space complexity as O(32 * N), emphasizing how the constant factor makes it linear relative to input size.

Key Points to Cover

  • Demonstrating the transition from O(N^2) to O(32N) complexity through bit manipulation
  • Explaining the greedy strategy of prioritizing opposite bits to maximize XOR results
  • Correctly handling the insertion and traversal logic within a Binary Trie structure
  • Acknowledging the fixed bit-width constraint (typically 31 or 32 bits) in the analysis
  • Articulating why this specific data structure is superior for bitwise operations compared to hash sets

Sample Answer

To solve the Maximum XOR problem efficiently, we first acknowledge that a brute-force comparison of all pairs results in O(N^2) time complexity, which is unacceptable for large datasets typical at Google. Instead, we can leverage a Binary Trie to achieve O(32N) time complexity. The core insight is that XOR yields 1 when bits differ. To maximize the result, we want the most significant bits of our answer to be 1 whenever possible. We start by inserting all numbers from the array into a Trie, where each node represents a bit position from the most significant bit down to the least significant. For each number in the array, we traverse the Trie to find the best match. At each bit level, we check if the opposite bit of our current number exists in the Trie. For example, if our current bit is 0, we prefer to go to the 1 child because 0 XOR 1 equals 1. If the preferred child exists, we move there and add 2^bit_position to our running maximum. If not, we are forced to take the same bit, resulting in a 0 for that position. By repeating this greedy traversal for every number, we ensure we find the global maximum without checking every pair. This approach demonstrates how understanding low-level data representation can drastically reduce algorithmic complexity, a skill highly valued in systems engineering.

Common Mistakes to Avoid

  • Attempting to implement a standard hash set instead of a Trie, missing the opportunity for bitwise optimization
  • Ignoring the direction of bit traversal, processing from least significant to most significant instead of MSB to LSB
  • Failing to handle negative numbers correctly by assuming unsigned integers only
  • Overlooking the edge case where the array contains duplicate values or a single element

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 87 Google questions