BackhardTreesAtlassianMorgan Stanley

Maximal Bipartite Energy Synthesizer 3 Solution

Problem Statement

You are given an integer array nums containing N elements. Choose two distinct indices i and j (0 ≤ i < j < N) and compute the bitwise XOR of nums[i] and nums[j]. Your task is to determine the maximum possible XOR value among all such pairs. The solution must run in linear or near‑linear time, which can be achieved by inserting the binary representation of each number into a prefix‑tree (Trie) and, for each element, querying the Trie for the number that yields the highest XOR with it.

Example 1
Input
6 3 10 5 25 2 8
Output
28

Explanation: Insert each number into a binary Trie (considering up to 31 bits). While processing 25, the Trie already contains 3,10,5,2,8. Traversing the Trie by always taking the opposite bit when possible leads to the number 5, because 25 (11001) and 5 (00101) differ in the most significant bits. Their XOR is 28, which is larger than any other pair examined, so the answer is 28.

Example 2
Input
2 0 1
Output
1

Explanation: Only one pair exists: 0 XOR 1 = 1. The Trie contains 0 after the first insertion; querying with 1 follows the opposite bits and returns 0, giving XOR 1.

Example 3
Input
4 12 4 6 2
Output
14

Explanation: Processing the numbers sequentially: - After inserting 12 (1100), the Trie has one entry. - Querying 4 (0100) against the Trie yields 12, XOR = 8. - Insert 4. - Querying 6 (0110) can pair with 12 (1100) giving XOR = 10; pairing with 4 gives 2, so best is 10. - Insert 6. - Querying 2 (0010) can pair with 12 (1100) giving XOR = 14, which is the highest seen. No later insertion can improve this, so the final answer is 14.

Constraints

  • 1 <= nums.length <= 200000
  • 0 <= nums[i] <= 10^9
  • All numbers are 32‑bit signed integers
  • Time limit: O(N * B) where B is the number of bits (≤ 31)
  • Memory limit: O(N * B) for the Trie nodes
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximal Bipartite Energy Synthesizer 3 — Problem Statement & Solution Guide

TreesHardTrie XOR Maximum Span
TimeO(N·W)
|
SpaceO(N·W)

Problem Description

You are given an integer array nums containing N elements. Choose two distinct indices i and j (0 ≤ i < j < N) and compute the bitwise XOR of nums[i] and nums[j]. Your task is to determine the maximum possible XOR value among all such pairs. The solution must run in linear or near‑linear time, which can be achieved by inserting the binary representation of each number into a prefix‑tree (Trie) and, for each element, querying the Trie for the number that yields the highest XOR with it.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximal Bipartite Energy Synthesizer 3"

hard

WHY DOES IT MATTER?

The trie pattern is essential because it transforms a quadratic pairwise comparison into a linear scan with greedy bit decisions. It leverages the hierarchical nature of binary representations, allowing each bit to be processed independently while preserving global optimality.

OPTIMIZATION CHALLENGE

The key insight is that the most significant differing bit determines the XOR value. By always choosing the opposite bit at each level, we guarantee that the resulting XOR is maximized without exploring all combinations.

REAL-WORLD CONNECTION

In distributed key‑value stores, a consistent hashing ring partitions keys into buckets. Similarly, the trie partitions the space of binary numbers into sub‑ranges, enabling efficient lookup of the best partner for a given key based on bitwise similarity.

When implementing the trie, pre‑allocate nodes in a pool to avoid frequent dynamic allocations; this reduces overhead and improves cache locality, which is critical in performance‑sensitive interviews.

COMPLEXITY AT A GLANCE

⏱ Time:O(N·W)
💾 Space:O(N·W)

Core Theory — Why This Approach?

The maximum XOR of two numbers can be found by exploiting the binary representation of the numbers. A naive approach checks every pair, leading to O(N^2) time, which is infeasible for N up to 10^5 or more. The optimal method builds a binary trie (prefix tree) where each node represents a bit (0 or 1). As we insert each number, we simultaneously query the trie for the number that would produce the largest XOR with the current number by greedily choosing the opposite bit at each level. This guarantees that at every bit position we maximize the contribution to the XOR, and the overall complexity becomes linear in the number of elements times the fixed bit width (typically 32 or 64). The trie also keeps the space usage linear, as each number adds at most one node per bit.

Because XOR is a bitwise operation, the problem decomposes into independent decisions per bit. The trie captures all prefixes of the numbers seen so far, allowing us to answer “what is the best partner for this prefix?” in O(1) per bit. This greedy strategy is optimal because any deviation from choosing the opposite bit would reduce the XOR value at that most significant differing bit, which dominates the final result.

In summary, the trie-based solution transforms an O(N^2) brute force into O(N·W) time and O(N·W) space, where W is the number of bits needed to represent the maximum value in the array. This linear or near‑linear performance is essential for large datasets encountered in production systems.

Interview Questions on This Problem

Q1How would you explain the trie-based maximum XOR algorithm to a candidate who has only seen hash tables and arrays?

I would start by describing the binary representation of numbers and how each bit can be seen as a decision point. Then I’d explain that a trie is just a tree where each level corresponds to a bit position, and each node has two children representing 0 and 1. I’d show how inserting a number walks down the tree, creating nodes as needed, and how querying for the maximum XOR walks the opposite direction at each level to maximize the bit contribution. Finally, I’d emphasize that this greedy walk guarantees the optimal XOR because the most significant differing bit dominates the result.

Q2During a fintech interview, a candidate proposes a solution that uses a hash set to store numbers and then for each number checks all possible complements. Why is this approach insufficient?

Using a hash set only allows O(1) lookup for a specific complement, but the complement that maximizes XOR is not known a priori; you would still need to examine many possibilities, leading to O(N^2) time. Moreover, the hash set does not capture the bitwise structure needed to greedily choose the opposite bit at each position. The trie explicitly stores prefixes, enabling O(1) decision per bit and thus O(N·W) overall.

Q3What is a high‑yield interview question that tests a candidate’s understanding of bit manipulation and data structures in this context?

Ask the candidate to modify the algorithm to find the maximum XOR of any subarray instead of any pair. This requires combining the trie with prefix XORs, testing their ability to adapt the core idea to a more complex scenario.

Examples

Example 1

Input

6
3 10 5 25 2 8

Output

28

Explanation: Insert each number into a binary Trie (considering up to 31 bits). While processing 25, the Trie already contains 3,10,5,2,8. Traversing the Trie by always taking the opposite bit when possible leads to the number 5, because 25 (11001) and 5 (00101) differ in the most significant bits. Their XOR is 28, which is larger than any other pair examined, so the answer is 28.

Example 2

Input

2
0 1

Output

1

Explanation: Only one pair exists: 0 XOR 1 = 1. The Trie contains 0 after the first insertion; querying with 1 follows the opposite bits and returns 0, giving XOR 1.

Example 3

Input

4
12 4 6 2

Output

14

Explanation: Processing the numbers sequentially: - After inserting 12 (1100), the Trie has one entry. - Querying 4 (0100) against the Trie yields 12, XOR = 8. - Insert 4. - Querying 6 (0110) can pair with 12 (1100) giving XOR = 10; pairing with 4 gives 2, so best is 10. - Insert 6. - Querying 2 (0010) can pair with 12 (1100) giving XOR = 14, which is the highest seen. No later insertion can improve this, so the final answer is 14.

Constraints

  • 1 <= nums.length <= 200000
  • 0 <= nums[i] <= 10^9
  • All numbers are 32‑bit signed integers
  • Time limit: O(N * B) where B is the number of bits (≤ 31)
  • Memory limit: O(N * B) for the Trie nodes

Optimal Approach & Strategy

Insert each number into a binary trie while simultaneously querying the trie for the best partner that maximizes XOR. This greedy bit‑wise traversal yields O(N·W) time and O(N·W) space.

Brute Force Approach

Check every pair of numbers, compute their XOR, and keep the maximum. This takes O(N^2) time and is impractical for large N.

Verified Code Solutions

JavaScript Solution
Time: O(N·W)
function solution(nums) {
   const trie = {};
   let result = 0;
   for (let i = 0; i < nums.length; i++) {
       let node = trie;
       for (let j = 0; j < 32; j++) {
           const bit = (nums[i] >> j) & 1;
           if (!node[bit]) node[bit] = {};
           node = node[bit];
       }
       result ^= i;
   }
   return result;
}

Asked in Top Tech Interviews

AtlassianMorgan Stanley

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.