Number of Steps to Reduce a Binary Number to One
Reported by candidates from Deloitte's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Deloitte OA reported in January 2022 hands you a binary string and one trap: the input can be too long to fit in an integer, so the obvious parse-and-loop approach breaks. It's a string scan from right to left with a carry, and it's short once you see it. If you blank on the carry logic mid-assessment, StealthCoder runs invisibly on your screen and gives you the working solution in real time. But the idea is small enough to hold in your head before you start. Here's the script.
The problem
You are given a binary string binary representing a positive integer. Repeatedly apply the following rule until the value becomes 1: If the current value is even, divide it by 2. If the current value is odd, add 1. Return the number of operations performed. The binary string may be too long for a built-in integer type, so process it without converting the complete value to an integer. Function numSteps(binary: String) → int Examples Example 1 binary = "1101" return = 6 The values are 13 -> 14 -> 7 -> 8 -> 4 -> 2 -> 1, which uses 6 operations. Example 2 binary = "10" return = 1 The value 2 is even, so one division produces 1. Example 3 binary = "1" return = 0 The value is already 1, so no operation is needed. Constraints 1 <= binary.length <= 500. binary contains only 0 and 1. binary[0] == '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the string from the last character down to index 1, tracking a carry. Zero bits cost one step if carry is 0 (divide). With a carry, a 0 becomes 1, so it costs two steps (add 1, then divide) and the carry stays 1. A 1 bit with no carry costs two steps (add 1, divide) and sets carry to 1. A 1 bit with carry becomes 0, so it costs one step (divide) and carry stays 1. At the end, if carry is 1 the leading 1 turns into 10, which adds one more step. The pitfall is skipping the leading bit and forgetting that final carry. Test with "1" (answer 0), "10" (answer 1) and "1101" (answer 6). If the carry rules scramble under pressure, StealthCoder is your hedge during the live OA, but the logic is only four cases.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Number of Steps to Reduce a Binary Number to One 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of steps to reduce a number in binary representation to one. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Deloitte's OA.
Deloitte 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.
Number of Steps to Reduce a Binary Number to One FAQ
How hard is the Deloitte binary-to-one problem really?+
Easy to medium. The simulation is trivial, but the string can be 500 characters long, so you can't convert it to an integer in many languages. The carry scan is the whole challenge, and it's about ten lines once you see it.
What's the trick to avoiding big integer conversion?+
Process bits from right to left with a carry flag. Each bit's cost depends only on the bit and the carry. That gives O(n) time and O(1) extra space, with no arithmetic on the full value.
What edge case breaks the naive solution?+
The input "1" must return 0, and a leftover carry at the end needs one extra step. Many people loop through index 0 too, which double counts. Stop at index 1, then add the carry.
Can I just simulate it with a big integer type?+
It works in languages with big integers, but the problem says to process without converting the whole value. Repeated halving and adding on strings is also slower. The carry scan is cleaner and safer to submit.
How do I prepare for this in 48 hours?+
Hand-trace "1101" with the carry rules until you get 6. Then code it and test "1", "10", "111" and "1000". That takes under an hour, and the rest of your time is better spent on other string and math patterns.