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.

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

Input

The function will take two inputs: the divisor and the dividend. The following is an example of these inputs:

dividend = 8
divisor = 2

Output

The 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.