Largest XOR Between Any Two IPv4 Addresses in a Log
Last updated: November 29, 2026
Quick Overview
Given a list of IPv4 addresses as 32-bit integers, return the maximum value of a XOR b over all pairs, in better than quadratic time.
0
0
0 solved
Given a list of IPv4 addresses as 32-bit integers, return the maximum value of a XOR b over all pairs, in better than quadratic time.
Asked in Google technical screens with a networking or Cloud flavor, where addresses are bit strings. The interviewer wants to see a binary trie built from the high bit down and a greedy walk that prefers the opposite bit.
What the Interviewer Expects
- Recognize the quadratic brute force and explain why it is too slow for millions of addresses
- Build a binary trie from bit 31 down to bit 0
- For each number, greedily choose the opposite bit when that branch exists
- Justify the greedy choice using the dominance of higher bits
- Handle a list with a single address
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 the maximum XOR of a new address against everything seen so far, online?
- How do you support deletions?
- Can you do it with a prefix-set approach instead of an explicit trie?
- How would you restrict pairs to addresses in the same /16 subnet?
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: nums, a non-empty list of integers in [0, 2^32). Output: the maximum of nums[i] ^ nums[j] over all i, j (with a single element the answer is 0). Target complexity is linear in n times...
High-Level Design
XOR is maximized by making the highest possible bit 1, then the next, and so on, because bit k is worth more than all lower bits combined. Insert every number into a binary trie keyed on bits from 3...