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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
binary trie
offline queries
sorting
bit manipulation
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported