Reported July 2025
Salesforcebit manipulation

Minimum Operations to Zero

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

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

The data structure this Salesforce OA hinges on is the binary representation of n, not a heap or a map. Reported in July 2025, "Minimum Operations to Zero" asks you to add or subtract powers of two until n hits 0. If you're taking it in the next day or two, the bit pattern is the whole game. Most people reach for BFS or DP, burn twenty minutes, and still miss the clean answer. The real solution is a short greedy loop over the bits. And if you blank mid-assessment, StealthCoder runs invisibly as a hedge and hands you the approach.

The problem

You are given an integer n. In one operation, you may add or subtract any power of two from n, such as 2^0, 2^1, 2^2, and so on.
Return the minimum number of operations required to reduce n to 0.

Function
getMinOperations(n: int) → int

Examples
Example 1
n = 21
return = 3
One optimal sequence is 21 -> 20 by subtracting 1, 20 -> 16 by subtracting 4, and 16 -> 0 by subtracting 16.

Constraints
1 <= n <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: look at the lowest bits of n. If n is even, shift right, since no operation is needed on a zero bit. If n is odd, you must spend one operation. Check n & 3. If it's 3 (binary ends in 11), add 1, which carries and collapses a run of ones into a single higher bit. If it's 1, subtract 1. Count each odd step and loop until n is 0. This is the same idea as non-adjacent form, which minimizes nonzero signed digits. The pitfall is plain popcount. For 21 it gives 3, which matches, but for 15 popcount gives 4 while the answer is 2 (16 - 1). Special-case n = 3 carefully: subtracting 1 then 2 and adding 1 then subtracting 4 both take two operations, so either works. Runtime is O(log n), with n up to 10^9. StealthCoder is your safety net if the carry logic slips under pressure 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 Minimum Operations 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 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

⏵ The honest play

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

Salesforce 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.

Minimum Operations to Zero FAQ

What's the trick in Minimum Operations to Zero?+

Treat n as bits. Even means shift right for free. Odd costs one operation, and you choose add 1 or subtract 1. If the low two bits are 11, add 1 to merge a run of ones through the carry. Otherwise subtract 1. Count the odd steps.

Why doesn't counting set bits work?+

Popcount ignores that adding a power of two can collapse a run of ones. Take 15, which is 1111. Popcount says 4 operations, but 16 - 1 takes 2. Any input with consecutive ones breaks the naive count, so the greedy carry rule is required.

How hard is this one really?+

Easy to code, tricky to see. The loop is about ten lines. The difficulty is realizing greedy on the low bits is optimal rather than DP or BFS. Once you've seen the 11 versus 01 rule, it takes a few minutes to write.

Can I solve it with BFS or DP instead?+

You could, but n goes up to 10^9, so a state search over values is too large without heavy pruning. The bit-based greedy runs in about 30 iterations. Use brute force only to sanity-check small n against your greedy answer.

How do I prepare for this in 48 hours?+

Hand-trace a few values like 21, 15, 7, and 3 through the n & 3 rule until it feels automatic. Then write the loop once from memory. Practice explaining why adding 1 on a trailing 11 never hurts. That covers this problem and its close variants.

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

OA at Salesforce?
Invisible during screen share
Get it