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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

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
binary trie
bit manipulation
greedy
XOR
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported