Compute a Quotient Using Signed 32-Bit Operations

Quick Overview

Compute a quotient truncated toward zero without multiply, divide, or modulo operators, keeping every stored integer within signed 32-bit limits.

Compute a Quotient Using Signed 32-Bit Operations

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the integer quotient of `dividend` and `divisor` without using multiplication, division, or remainder/modulo operators. Discard the fractional part toward zero: a negative quotient is rounded upward toward zero, not downward. The environment can store only signed 32-bit integers, from `-2147483648` through `2147483647`. If the mathematical quotient is above that range, return `2147483647`; if it is below the range, return `-2147483648`. Wider signed or unsigned integer storage is not available. Preserve the original interface: ```cpp int divide(int dividend, int divisor); ``` ### Examples ```text Input: dividend = 29, divisor = 4 Output: 7 ``` The discarded fractional part does not increase the quotient to 8. ```text Input: dividend = -37, divisor = 5 Output: -7 ``` Truncation toward zero produces `-7`, rather than the floor value `-8`. ### Constraints - Both inputs lie between `-2147483648` and `2147483647`, inclusive. - `divisor` is nonzero. - Multiplication, division, and modulo/remainder operators may not be used. - Stored integer values, including arithmetic intermediates, must remain within the signed 32-bit range.

Overview: Compute a quotient truncated toward zero without multiply, divide, or modulo operators, keeping every stored integer within signed 32-bit limits.

Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the integer quotient of dividend and divisor without using multiplication, division, or remainder/modulo operators. Discard the fractional part toward zero: a negative quotient is rounded upward toward zero, not downward.

The environment can store only signed 32-bit integers, from -2147483648 through 2147483647. If the mathematical quotient is above that range, return 2147483647; if it is below the range, return -2147483648. Wider signed or unsigned integer storage is not available.

Preserve the original interface:

int divide(int dividend, int divisor);

Examples

Input: dividend = 29, divisor = 4
Output: 7

The discarded fractional part does not increase the quotient to 8.

Input: dividend = -37, divisor = 5
Output: -7

Truncation toward zero produces -7, rather than the floor value -8.

Constraints

  • Both inputs lie between -2147483648 and 2147483647 , inclusive.
  • divisor is nonzero.
  • Multiplication, division, and modulo/remainder operators may not be used.
  • Stored integer values, including arithmetic intermediates, must remain within the signed 32-bit range.