Maximum XOR With a Capped Value for Batch Network Queries
Last updated: November 29, 2026
Quick Overview
Given an array of non-negative integers and queries (x, m), answer for each query the maximum of x XOR a over array elements a with a <= m, or -1 if no element qualifies.
0
0
0 solved
Given an array of non-negative integers and queries (x, m), answer for each query the maximum of x XOR a over array elements a with a <= m, or -1 if no element qualifies.
A hard Google onsite bit trick problem. The interviewer wants you to see that the cap makes a plain binary trie insufficient and to answer queries offline, sorted by cap, inserting elements as the cap grows.
What the Interviewer Expects
- Explain why a static trie ignores the cap
- Sort elements and queries by cap and insert incrementally
- Answer each query with the greedy opposite-bit walk
- Restore answers to the original query order
- Mention the online alternative of storing subtree minimums
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 queries online without sorting them?
- What if elements can be deleted between queries?
- How would you add a lower bound a >= lo as well?
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 with values in [0, 2^30), and queries, a list of pairs (x, m). Output: a list where entry q is max(x ^ a) over a in nums with a <= m, or -1 if none. Up to 10^5 elements a...
High-Level Design
Without the cap, a binary trie with greedy opposite-bit choice answers each query in 30 steps. The cap means each query sees a different subset. Answering offline fixes this: sort nums, sort query i...