DIY: Divide Two Integers

Solve the interview question "Divide Two Integers" in this lesson.

Problem statement

For this problem, you are given two integer variables. The first variable is the Dividend and the second variable is the Divisor. Your task is to implement the Divide() function.

Assume the following constraints:

  • 231-2^{31} <= Dividend, Divisor <= 2312^{31} - 1
  • The divisor is guaranteed to be non-zero.

Hint: We should multiply positive input values by -1 to convert them into negative numbers. This prevents the possibility of a negative overflow.

Input

The Divide() function will take two inputs: the Divisor and the Dividend. The following is an example of these inputs:

dividend = 8
divisor = 2

Output

The Divide()function will return a quotient. The following is the output of the inputs given above:

4

Coding exercise

For this coding exercise, you need to implement the Divide(Dividend, Divisor) function, wherein the Dividend and Divisor are both integer variables.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.