Reported May 2024
Uberbit manipulation

Maximize XOR for Each Query

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

Uber hit you with a bit-manipulation problem in May 2024, and XOR queries are a classic pattern they cycle through. You're given some data structure and a series of queries where you need to find the maximum XOR result. This is the kind of problem where the naive approach tanks on larger inputs, but the trick is often a trie or prefix tree built on binary representations. StealthCoder will catch you if you blank on the structure mid-implementation.

Pattern and pitfall

The core pattern here is building a binary trie where each bit position becomes a branch. You insert numbers into this trie by their bit representation, then for each query, you greedily traverse to maximize XOR at every step. If you want max XOR, you pick the opposite bit at each level when it exists. The pitfall is thinking you can brute-force compare every number to every query, which fails on large datasets. A trie gives you O(log max_num) per query after O(n log max_num) preprocessing. StealthCoder handles the trie construction and traversal if you freeze up during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Maximize XOR for Each Query cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum xor of two numbers in an array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximize XOR for Each Query FAQ

Is this actually a trie problem or can I use something simpler?+

Trie is the intended solution for optimal time. You could brute-force O(n*q) but Uber's test cases are designed to fail that. Trie is the pattern they're testing. Build it with two branches per node (0 and 1) and traverse greedily.

How do I handle the binary conversion without bugs?+

Convert each number to a 32-bit (or 64-bit) binary representation. Iterate from the most significant bit down. For each bit, decide if you go left (0 branch) or right (1 branch). Most languages have bit-shift operators, so use (num >> i) & 1 to extract the i-th bit.

What's the trick to maximize XOR during traversal?+

At each bit level, XOR returns 1 when bits differ. So for max XOR, try to go the opposite direction of the query's current bit. If the query has a 0 at position i, go to the 1 branch if it exists. Always prefer the opposite.

Can I solve this without a trie?+

Theoretically yes but it's slow. You could use a set and iterate through all numbers for each query, comparing XOR values. Works for tiny inputs but Uber's test cases have enough queries and numbers to timeout. Trie is the expected approach.

How many test cases should I expect to pass with a working trie solution?+

A correct trie-based solution with proper binary traversal should pass all visible test cases. If you're failing, check your bit extraction logic, your trie construction, and that you're always preferring the opposite bit during query traversal.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it