Divide Two Integers Without Using Multiply, Divide or Mod
Last updated: November 29, 2026
Quick Overview
Compute the truncated quotient of two 32-bit signed integers using only addition, subtraction and bit shifts, clamping the single overflow case to the maximum 32-bit value.
Microsoft
November 29, 20260
0
0 solved
Compute the truncated quotient of two 32-bit signed integers using only addition, subtraction and bit shifts, clamping the single overflow case to the maximum 32-bit value.
A Microsoft technical screen question that tests binary long division and careful overflow reasoning, topics that come up for teams working close to the runtime, like .NET and compilers. The interviewer looks for O(log n) shifting rather than repeated subtraction and for explicit handling of INT_MIN / -1.
What the Interviewer Expects
- Explain why repeated subtraction is too slow
- Use shifted copies of the divisor to subtract large chunks
- Determine the sign separately and work with magnitudes
- Handle INT_MIN divided by -1 by clamping
- Truncate toward zero
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you implement this with only 32-bit arithmetic, without abs on INT_MIN?
- How would you also return the remainder?
- How do hardware dividers relate to this algorithm?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Both inputs are 32-bit signed integers and the divisor is non-zero. The quotient truncates toward zero, so 7 / -3 is -2. The only result that does not fit in 32 bits is -2^31 / -1, which should re...
High-Level Design
Subtracting the divisor one copy at a time takes up to 2^31 steps. Binary long division takes 32: for each bit position from high to low, check whether b << shift fits into the remaining dividend; i...