Integer Replacement
A medium-tier problem at 37% community acceptance, tagged with Dynamic Programming, Greedy, Bit Manipulation. Reported in interviews at Baidu and 0 others.
Integer Replacement is a medium-difficulty problem that catches candidates off guard because the greedy path looks right but isn't. You're given an integer n and three operations: subtract 1, divide by 2 if even, or add 1 if odd. The goal is the minimum number of steps to reach 1. Baidu has asked this. The acceptance rate sits at 36.5%, which signals a real trick. Most people start with a simple greedy approach and hit a wall when the math doesn't work. If this lands in your live assessment and you blank on the pattern, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Integer Replacement"
Integer Replacement is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe trap is thinking greedy always wins: divide by 2 when you can, subtract 1 when you can't. That fails because adding 1 before dividing can sometimes save steps overall. The real move is bit manipulation plus memoization or dynamic programming. When n is odd, you have a choice: add 1 or subtract 1. The trick is recognizing that adding 1 can cascade into multiple free divisions, especially when n+1 has trailing zeros in binary. Classic setup: n = 3 seems like subtract 1 twice, but 3+1=4 divides to 2 divides to 1 in just two steps. You need either BFS to explore both paths or memoized recursion to cache results. This is where DP and greedy collide. StealthCoder handles the bit-math reasoning for you during the live session if you're stuck.
Pattern tags
You know the problem.
Make sure you actually pass it.
Integer Replacement recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Integer Replacement interview FAQ
Why doesn't pure greedy work on Integer Replacement?+
Greedy fails because adding 1 to an odd number can create a higher power of 2, leading to more divisions. For n=3: subtract gives 2,1 (two steps), but add gives 4,2,1 (two steps total). At larger scales, add-then-divide beats subtract. The optimal choice depends on the binary structure, not just divisibility.
What's the bit manipulation insight?+
When n is odd, check the last two bits. If n&3==1, subtract 1. If n&3==3, add 1 (usually). This is because adding 1 to numbers ending in 11 in binary creates a carry that gives more trailing zeros, enabling faster division chains. Memoization then caches these decisions.
Is this still asked at top tech companies?+
Baidu has reported asking it. The 36.5% acceptance rate shows it's niche but real. It appears in problem banks focused on bit manipulation and DP. Not as common as LeetCode mediums, but it's exactly the kind of curveball that appears in onsites or harder assessments.
Do I need BFS or recursion with memoization?+
Either works. BFS explores both paths level by level until you hit 1, guaranteeing the shortest path. Memoized recursion is cleaner if you're comfortable with top-down DP. Both need to track visited states to avoid infinite loops. Recursion with a memo dict is typically faster for this problem.
How does this relate to Dynamic Programming and Greedy?+
Integer Replacement teaches that greedy doesn't always minimize steps in path problems. DP solves it by exploring all branches and caching results. It's a hybrid: you can use greedy heuristics (the bit check) inside a DP solution. That's the actual pattern worth knowing for harder DP/greedy crossovers.
Want the actual problem statement? View "Integer Replacement" on LeetCode →