DIY: Divide Two Integers
Solve the interview question "Divide Two Integers" in this lesson.
We'll cover the following
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:
- <=
dividend
,divisor
<= - 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_integers()
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_integers()
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_integers(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.