Maximum XOR of Any Two Device IDs Using a Binary Trie
Last updated: November 29, 2026
Quick Overview
Given a list of non-negative 32-bit device IDs, return the maximum value of a XOR b over all pairs, in better than quadratic time.
Microsoft
November 29, 20260
0
0 solved
Given a list of non-negative 32-bit device IDs, return the maximum value of a XOR b over all pairs, in better than quadratic time.
Appears in Microsoft online assessments where the brute force times out by design. It tests whether the candidate can build a trie over bits and answer greedy "take the opposite bit if it exists" queries, a combination of bit manipulation and prefix structures.
What the Interviewer Expects
- Explain why the brute force O(n^2) fails at scale
- Insert numbers into a binary trie from the most significant bit
- Greedily choose the opposite bit at each level when possible
- Query each number against those already inserted
- State O(n * B) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you answer max XOR of a query value against only IDs below a limit?
- Can you solve it with a prefix hash set instead of a trie?
- How would you find the maximum XOR of any subarray?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a list of n non-negative integers below 2^32, n up to around 10^5. Return the maximum of a ^ b over pairs (a single element gives 0). Quadratic pair checking is 10^10 operations, so we need...
High-Level Design
XOR is maximized greedily from the top bit down: a 1 in bit 31 is worth more than all lower bits combined. Store numbers in a binary trie, most significant bit first. To find the best partner for x, w...