Maximum XOR Between Any Two User ID Hashes
Last updated: November 29, 2026
Quick Overview
Given a list of non-negative 31-bit integers representing hashed user IDs, return the maximum value of a XOR b over all pairs in the list.
Meta
November 29, 20260
0
0 solved
Given a list of non-negative 31-bit integers representing hashed user IDs, return the maximum value of a XOR b over all pairs in the list.
Meta onsites sometimes use a bitwise trie question to see whether a candidate can move beyond the O(n^2) pair scan. The interviewer wants the insight that a greedy walk from the highest bit, preferring the opposite bit, yields the best partner for each number.
What the Interviewer Expects
- Reject the O(n^2) brute force with a reason
- Build a binary trie keyed from the most significant bit
- Greedily pick the opposite bit at each level when available
- Query each number against the trie built so far
- Explain why greedy from the top bit is optimal
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 with numbers up to a given limit?
- How would you support deleting numbers from the set?
- Can you solve it with a prefix-set approach instead of a trie?
- How would the bit width change memory and time?
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: list of integers in
[0, 2^31), at least one element. A single element pairs with itself for XOR 0. - Output: maximum XOR over pairs
(i, j). - Target O(n * B) with
B = 31bits....
High-Level Design
A higher bit outweighs all lower bits combined, so for a given number x the best partner is the one that differs from x at the highest possible bit, then the next, and so on. A binary trie over th...