DSA Tracker

Medium

Maximum XOR of Two Numbers in Array

A medium Trie problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Trie
Sheets
2
Core for
7 roles
Platform
LeetCode

The problem

Given an array of integers, find the maximum result of XORing any two numbers in the array.

Example 1

Input
nums = [3, 10, 5, 25, 2, 8]
Output
28
Why
The maximum XOR is 5 XOR 25 = 28 (binary: 00101 XOR 11001 = 11100).

Example 2

Input
nums = [2, 4]
Output
6

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • 0 <= nums[i] <= 2^31 - 1

How to think about it

Updated 2026-09-09

The highest bit dictates value more than all lower bits combined. If a number has a 0 at bit position b, pairing it with a number that has a 1 at bit b is strictly superior to matching any lower bit. Storing binary representations in a 2-child trie lets each query greedily steer down the opposite bit branch whenever one exists.

Approaches, worst first

  1. All pairs check

    time O(n^2) · space O(1)

    Compute the XOR between every distinct pair of elements and retain the maximum. Correct by construction and memory-free, but quadratic runtime times out on twenty thousand elements.

  2. Prefix bitmask set

    time O(31 * n) · space O(n)

    Iterate bit by bit from bit 30 down to 0. Keep a running prefix mask, gather all masked prefixes into a hash set, and test if candidate prefixes can combine to yield the target bit. Runs in linear time relative to input size.

  3. Binary bitwise trieWrite this one

    time O(31 * n) · space O(31 * n)

    Insert each integer as a 31-bit path into a trie with children 0 and 1. For each number, traverse the trie while greedily following the inverted bit at each depth, yielding the optimal partner for each element in single passes.

Where people lose marks · 3
  • Starting bit traversal from bit 30 when numbers reach up to 2^31 - 1, missing the significance of the highest power-of-two bit.
  • Assuming a single-element array has a pair; when nums has length 1, no two distinct indices exist to XOR.
  • Using 32-bit signed bit shifts like candidate >> i in languages where negative numbers sign-extend, leading to unintended high bits.

The theory behind it

Trie — the ground this problem stands on. All Trie problems

What Trie is

A trie, also called a prefix tree, is a tree structured for storing words character by character. Instead of storing entire words in individual nodes, each step down a branch represents a single letter. Words that share the same beginning, like car, card, and care, share the exact same starting path down the tree. A special boolean marker sits at the end of each valid word to show that a complete word terminates at that letter.

When to reach for it

Reach for a trie when questions involve prefix lookups, dictionary word searches, autocomplete engines, or matching prefixes against a body of text. Prompts asking whether any word in a dictionary begins with a given prefix, or searching for words on a Boggle board grid, point directly to a trie. It also applies to bitwise tasks, such as finding the maximum XOR pair among integers by treating numbers as binary prefixes.

How the pattern works

Represent each trie node with an array or hash map of child links, plus a boolean flag marking if a complete word ends at that node. When inserting, start at the root and walk down character by character, creating new child nodes whenever a path does not exist yet, then mark the final node as a word ending. When searching, follow the characters; if any child link is missing, the word or prefix does not exist. If all characters match, check the boolean flag to distinguish between a full word and a partial prefix.

What each operation costs

OperationTime
insert word of length l into trieO(l)
search for full word of length lO(l)
check if any word begins with prefix of length lO(l)
What usually goes wrong with Trie
  • Confusing prefix matches with full word matches by returning true when characters exist but the end-of-word flag on the final node was never set.
  • Allocating fixed 26-slot arrays for child pointers without verifying that all input characters are strictly lowercase English letters.
  • Forgetting to prune unvisited branches during board searches, leading to time limit exceeded errors on grids with large word sets.

Which roles need this problem

Trie is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 2 more roles, including Information Retrieval Engineer, Storage Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Trie problems

Problem set and role mapping as of .