Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Divide Without Operators

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Divide Without Operators. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

FNZ platform calculations may require integer division for normalized portfolio values without relying on language division helpers. Implement integer division using bit shifts and subtraction only.

Given two signed 32-bit integers, dividend and divisor, return the quotient truncated toward zero. Do not use /, //, %, *, **, divmod, or equivalent library functions. You may use comparisons, addition, subtraction, and bit shifts.

If the result is outside the signed 32-bit range, clamp it to 2147483647 or -2147483648. The divisor is guaranteed to be nonzero.

Formal Specification

  • Input: two integers, dividend and divisor, each in [-2147483648, 2147483647].
  • Output: an integer equal to truncation toward zero of dividend / divisor, clamped to the signed 32-bit range.

Constraints

  • -2147483648 <= dividend <= 2147483647
  • -2147483648 <= divisor <= 2147483647
  • divisor != 0
  • The result must be truncated toward zero
  • Division, multiplication, modulo, exponentiation, divmod, and equivalent helpers are prohibited

Function Signature

def divide(dividend, divisor):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output