PPractick

DSA interview question

Maximum XOR of Two Numbers in an Array

Hard · Data structures · Topics: trie, bit manipulation

The problem

You're building a hash bucket distribution checker for a distributed cache system. To minimize collisions, your team wants to verify the maximum possible XOR between any two cache key identifiers in a given batch — a high max-XOR value is a good signal of well-distributed bits across the bucket space. Given an integer array nums, find the maximum result of nums[i] XOR nums[j] for any valid i != j.

Example: nums = [3,10,5,25,2,8] → maximum XOR is 5 XOR 25 = 28.

Function signature

int findMaximumXOR(vector<int>& nums);

Test cases

Input numsOutputExplanation
[3,10,5,25,2,8]285 (00101) XOR 25 (11001) = 28 (11100)
[0]0Only one element — no valid pair, conventionally returns 0
[2,4]62 (010) XOR 4 (100) = 6 (110)
[8,10,2]108 (1000) XOR 2 (0010) = 10 (1010)
[14,70,53,83,49,91,36,80,92,51,66,70]127Larger test case from LeetCode's examples
[0,0]0Identical elements XOR to 0
[1,2,3]31 XOR 2 = 3, the maximum among all pairs

How interviewers phrase it

Clarifying questions to ask

  1. Can i == j (i.e., can I XOR an element with itself)? (Typically no — needs i != j, though XOR-ing identical values just gives 0 anyway, so it rarely changes the answer, but still good to confirm logically.)
  2. What's the range of values in nums? (Determines how many bits the Trie needs to consider — e.g., up to 2^31 - 1 needs 31 bits.)
  3. Can nums contain duplicates or negative numbers? (Standard LeetCode version assumes non-negative; clarify if negatives are in scope, since bitwise XOR semantics with two's complement negatives need extra care.)
  4. What should be returned for a single-element or empty array? (Convention: 0, since no valid pair exists — confirm with interviewer.)

Follow-ups to expect

  1. "What's the brute force approach, and why is it insufficient?"
  2. "Walk through why the Trie approach is O(n * bitLength) instead of O(n²).
  3. "Can you solve this without a Trie, using a hash-set-based bit-by-bit greedy approach instead?"
  4. "How does this relate to the query-based version you solved earlier (Max XOR With an Element From Array)?"
  5. "What if you needed the actual PAIR (i, j) achieving the max XOR, not just the value?"
  6. "How would you extend this to find the maximum XOR of THREE numbers, not two?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in