29. Divide Two Integers

Practice Divide Two Integers for free on NudgeCode: Medium, Bit Manipulation pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Divide the signed 32-bit integer dividend by the nonzero signed 32-bit integer divisor without using multiplication, division or the modulo operator. Truncate the quotient toward zero (drop the fractional part). The answer must fit in a signed 32-bit integer: if the true quotient is greater than 2^31 - 1, return 2^31 - 1; if it is less than -2^31, return -2^31.

Example

dividend = 10, divisor = 3            ->  3
dividend = 7, divisor = -3            ->  -2   (-2.33 truncates to -2)
dividend = -2147483648, divisor = -1  ->  2147483647   (2^31 does not fit)

Constraints