Skip to content
Decoder

Divide Two Integers

Medium

Problem

Divide dividend by divisor without using multiplication, division, or the modulo operator, and return the quotient truncated toward zero (so 8.7 becomes 8 and -2.3 becomes -2).

The result must fit in a signed 32-bit integer: if the true quotient is greater than 2^31 - 1, return 2^31 - 1.

Example 1

Input
dividend = 10, divisor = 3
Output
3

Example 2

Input
dividend = 7, divisor = -3
Output
-2

Example 3

Input
dividend = -2147483648, divisor = -1
Output
2147483647
Why
clamped

Constraints

  • -2^31 <= dividend, divisor <= 2^31 - 1
  • divisor != 0

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.