Trains the technique from
LeetCode 29Divide Two IntegersThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
You are writing microcode for a small motor controller. Its arithmetic unit can add, subtract, compare, and shift a register left or right, and that is the whole instruction set: there is no multiply instruction, no divide instruction and nothing that hands back a remainder. The firmware still needs a divide step, so you have to build one.
Given two signed integers value and step, report how many whole steps of size step go into value, carrying the sign the pair of operands implies. Anything left over is thrown away, so the count is cut towards zero and never rounded away from it: 19 over 5 gives 3, and -19 over 5 gives -3.
The result is latched into a signed 32-bit register, which holds values from -2^31 up to 2^31 - 1. Should the cut count sit above that ceiling, latch the ceiling 2^31 - 1 instead; should it sit below the floor, latch the floor -2^31 instead. The caller never passes a step of zero.
Build the count from addition, subtraction, comparison and bit shifts only, the way the unit would. Do not reach for a multiply, divide or remainder operator anywhere in your answer.
Example 1
Five steps of size 4 fit inside 23 and 3 is left over. The operands carry opposite signs, so the count is negative and the leftover is discarded: -5.
Example 2
Four steps of size 7 fit inside 30 with 2 left over, and the opposite signs make the count -4.
Example 3
Two negative operands give an exact count of 2147483648, which is one above what the register holds, so the ceiling 2147483647 is latched.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def microcode_divide_step(value: int, step: int) -> int:public int microcodeDivideStep(int value, int step)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.