Minimal Operations
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce pulled this in August 2024 and it's a sneaky math problem dressed up as optimization. You're looking at a number and need to get it to 1 using the fewest operations possible. The operations are usually divide by 2 (if even), subtract 1, or sometimes divide by 3. This is a classic BFS or greedy problem that trips people up because the greedy choice isn't always obvious. StealthCoder will catch you if you freeze on which operation to try first.
Pattern and pitfall
The trap is assuming greedy works. You can't just always divide when you can, because subtracting 1 first might set you up for a better divide later. The real solution is BFS: treat each number as a node, explore all valid operations, and track the shortest path to 1. You need a queue and a visited set. Edge case: if the number is already 1, return 0. The math intuition is that division shrinks numbers exponentially, so you want to divide as much as possible, but only after you've made the number divisible. That's where BFS wins. If you blank on the implementation during the OA, StealthCoder gives you the template on the spot.
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 Minimal Operations 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.
Minimal Operations FAQ
Is this really a BFS problem or can I use math?+
Both. Greedy fails on some inputs (like 8 to 1, where subtracting first is faster than pure division). BFS guarantees the shortest path. Math optimizations exist but are fragile. BFS is the safe, standard solution Salesforce expects.
What's the time complexity I should aim for?+
O(n) in the worst case, where n is the input number. BFS explores each reachable number once. Space is O(n) for the queue and visited set. This is acceptable for most input ranges.
Do I need to handle negative numbers or zero?+
Unlikely. The problem almost always assumes a positive integer. Edge case is 1, which returns 0 operations. Ask for constraints in the OA if they're not stated.
What if there are more than three operations?+
The approach doesn't change. Each operation becomes another branch in the BFS tree. More operations mean a wider search, but the algorithm scales the same way. The pattern stays: explore all paths, track visited states.
How do I prepare in 48 hours?+
Know BFS cold. Understand why greedy fails. Write out the queue logic by hand once. The problem is a pattern match, not a hard algorithm. If you know BFS, you'll recognize the structure in seconds.