System Energy Reduction
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive answer on Salesforce's System Energy Reduction, reported in March 2026, is a run of consecutive 1 bits. Just counting set bits gives 3 for n = 7, and the real answer is 2. You add 1, hit 8, and subtract 8. That's the whole question: you can add or subtract any power of two, and you need the fewest moves to reach 0. It's a bit manipulation and greedy problem dressed up with a corporate name. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the approach in real time, but the logic below is short enough to own.
The problem
You are given an integer n, where 0 < n < 2^60. In one operation, you may add or subtract any power of two from n. That is, you may choose an integer k >= 0 and replace n with either n + 2^k or n - 2^k. Return the minimum number of operations needed to reduce n to 0. Function minimumEnergyReductionOperations(n: long) → int Examples Example 1 n = 7 return = 2 Add 1 to get 8, then subtract 8 to reach 0. Example 2 n = 10 return = 2 Subtract 8, then subtract 2. Constraints 0 < n < 2^60
Reported by candidates. Source: FastPrep
Pattern and pitfall
Think of the minimum number of signed powers of two that sum to n. That's the non-adjacent form (NAF) of n, and its nonzero digit count is the minimum. Greedy walk: while n > 0, if n is even, shift right. If n is odd, look at the low two bits. If n % 4 == 1, subtract 1. If n % 4 == 3, add 1, which carries through the run of ones. Count one operation per odd step. For n = 7, you get 7 -> 8 -> 0, so 2 operations. The pitfall is popcount, which overcounts runs like 0111. A second pitfall is overflow: n is under 2^60, so use 64-bit longs, since adding 1 can push you to 2^60. Edge case n = 3 gives 2 either way (4-1, or 2+1). Complexity is O(log n) time and O(1) space.
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 System Energy Reduction 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 Salesforce's OA.
Salesforce 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.
System Energy Reduction FAQ
What's the trick in System Energy Reduction?+
Minimize the count of signed powers of two summing to n. At each odd step, check n mod 4. If it's 3, add 1 so the run of ones collapses into a single carry. If it's 1, subtract 1. Even numbers just shift right at no cost.
Why doesn't counting set bits work?+
Because subtraction is allowed. n = 7 is 111 in binary, three set bits, but 8 - 1 does it in two operations. Any run of two or more ones can be replaced by one add and one subtract, which is often cheaper than touching each bit.
Is there an edge case with n = 3?+
Yes. 3 is 11 in binary. Subtracting 1 then 2 takes two operations. Adding 1 then subtracting 4 also takes two. Both paths tie, so your greedy must not break here. Test it explicitly along with n = 1, which returns 1.
Do I need to worry about overflow with n up to 2^60?+
Use a 64-bit type. Adding 1 to a number near 2^60 stays well within a signed long, but an int will fail instantly. In languages with fixed-width ints, declare n as long and use bit shifts or division on that type.
How do I prepare for this in 48 hours?+
Write the n mod 4 greedy from scratch and trace n = 7, 10, 11, 15, and 23 by hand. Then compare it against a brute-force BFS on small n to confirm it matches. That's about an hour of work and covers the whole pattern.