Binary Reduction
Medium · Online assessment (OA) · Full-time
Got this in the OA for an AI Engineer role. They gave me a binary string representing a non-negative number, and I had to return how many operations it takes to reduce it to zero by subtracting one if odd or dividing by two if even. I worked directly on the binary representation instead of converting to an integer, since the string could be way too large for that. Dividing by two is just dropping the last bit, and subtracting one from an odd number flips the last bit to zero, so I walked through the string from right to left counting operations. The example 011100 worked out cleanly once I had the bit manipulation logic down.
