Minimum Operations To Reduce To Zero
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber reported this one in June 2026, and it looks like a math puzzle until you see what it really is: the fewest signed powers of two that sum to n. That's the non-adjacent form of the binary representation, and it's a bit trick, not a search problem. If you've got an OA invite and 48 hours, this is worth ten minutes of your time. Most people reach for BFS or DP on n up to 2^40 and die. If you blank on the day, StealthCoder is the invisible safety net that reads the problem and hands you the approach.
The problem
Given a positive integer n, you can either add or subtract 2^i in a single operation where i >= 0. Determine the minimum number of operations required to reduce n to 0. Find the minimum number of operations required to convert n to 0. Constraints 1 <= n < 2^40 Function getMinOperations(n: long) → int Examples Example 1 n = 6 return = 2 n can be reduced to 0 using two operations: Choose i = 1 and subtract 2^1 from 6, so 6 - 2 = 4. Choose i = 2 and subtract 2^2 from 4, so 4 - 4 = 0. The answer is 2. Example 2 n = 21 return = 3 One sequence of operations is: Choose i = 0, subtract 2^0 = 1 from 21, converting it to 20. Choose i = 2, subtract 2^2 = 4 from 20, converting it to 16. Choose i = 4, subtract 2^4 = 16 from 16, converting it to 0.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Every operation adds or subtracts a power of two, so you're writing n as a sum of signed powers of two with as few terms as possible. Walk the bits from the lowest. If the current bit is 0, shift. If it's 1, look at the next bit. If the next bit is also 1 (a run of ones), add 1 so the run carries up, and count one operation. If it's an isolated 1, subtract it and count one operation. The special case is n = 3, where subtracting 1 then 2 and adding 1 then subtracting 4 both cost 2, so either works. Equivalent greedy: while n > 0, if n is odd, check n & 3 == 3 and n != 3 to choose add, else subtract. Pitfall: brute-force recursion or BFS blows up at 2^40. Use long, not int. If you freeze live, StealthCoder is the hedge that surfaces this bit walk.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minimum Operations To Reduce To Zero 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Operations To Reduce To Zero FAQ
What's the trick in Minimum Operations To Reduce To Zero?+
Treat it as the minimum count of signed powers of two summing to n. Scan bits from the bottom. A run of ones is cheaper to handle by adding 1 and carrying than by subtracting each bit. Isolated ones get subtracted. It's the non-adjacent form, so the answer is linear in the bit length.
Why not just use the popcount of n?+
Popcount only counts subtractions. For 7 (111), popcount gives 3, but 8 - 1 takes 2 operations. Allowing additions lets runs of ones collapse into one higher bit plus one correction. That's why the answer can be lower than the number of set bits.
Can I solve it with BFS or DP?+
Not safely. n goes up to 2^40, so a state space over values is far too big. A memoized recursion that tries n minus or plus the nearest powers can work, since only the floor and ceiling power matter, but the bit greedy is simpler and O(log n).
What edge cases should I test?+
Test n = 1 (answer 1), n = 3 (answer 2), powers of two (answer 1), and all-ones values like 7 and 15 (answer 2). Also test a large value near 2^40 to confirm you're using a 64-bit long and nothing overflows during the carry.
How do I prepare for this in 48 hours?+
Work three or four examples by hand, like 6, 21, 7 and 23, and write the odd-check with n & 3 == 3. Code the loop once, then run it against a brute-force BFS on small n to confirm. That's enough. The whole solution is under ten lines.