Maximum XOR Between Any Two Device Serial Numbers

Last updated: November 29, 2026

Quick Overview

Given a list of non-negative 31-bit serial numbers for Echo devices in a firmware rollout cohort, return the largest value of a XOR b over all pairs. Aim for better than the O(n^2) pairwise check.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a list of non-negative 31-bit serial numbers for Echo devices in a firmware rollout cohort, return the largest value of a XOR b over all pairs. Aim for better than the O(n^2) pairwise check.

An Amazon onsite coding question where the brute force is obvious and the interviewer pushes for the bitwise trie or the prefix-set greedy. They watch how you build the answer bit by bit from the most significant bit and whether you justify the greedy choice.

What the Interviewer Expects
  • Explains why greedy on the highest bit is optimal
  • Builds a binary trie or uses the prefix-set method
  • Queries the opposite bit at each level
  • Gives O(n * B) time
  • Handles a single element (answer 0)
Key Topics to Cover
binary trie
greedy by bit
XOR
prefix sets
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
  • Answer many queries: max XOR of x with any element at most m.
  • How would you support deletions from the set?
  • What is the memory footprint of the trie for 10^5 numbers?
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: list of n non-negative integers below 2^31. Output: max of a ^ b over pairs (a may equal b, giving 0). n up to about 10^5, so O(n^2) is too slow....

High-Level Design

A higher bit outweighs all lower bits combined, so we decide the answer from bit 30 down to bit 0 greedily: at each bit, can we set it given the bits already chosen? The prefix-set approach keeps the ...


Submit Your Answer
Markdown supported