PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in implementing low-level integer arithmetic and algorithmic optimization, including correct handling of overflow and edge cases within 32-bit signed integer constraints.

  • medium
  • Amazon
  • Coding & Algorithms
  • Machine Learning Engineer

Implement integer division without using division

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given two 32-bit signed integers `dividend` and `divisor`. Implement a function that divides `dividend` by `divisor` and returns the **integer quotient**, **without** using the multiplication `*`, division `/`, or modulo `%` operators. The result should be truncated toward zero (i.e., rounded toward 0, not toward −∞ or +∞). Assume: - `divisor` is not zero. - The range of 32-bit signed integers is from −2³¹ to 2³¹ − 1. You must: - Handle potential overflow correctly. If the result overflows the 32-bit signed integer range, return `2^31 - 1` (the maximum 32-bit signed integer). - Aim for a time complexity **better than O(|dividend|)** (i.e., you should not subtract `divisor` from `dividend` one unit at a time). **Function signature example (language-agnostic):** ```text int divide(int dividend, int divisor); ``` Explain the algorithm you would use and then implement the function in the programming language of your choice.

Quick Answer: This question evaluates proficiency in implementing low-level integer arithmetic and algorithmic optimization, including correct handling of overflow and edge cases within 32-bit signed integer constraints.

Given two 32-bit signed integers dividend and divisor, implement integer division and return the integer quotient. You may not use the multiplication (*), division (/), or modulo (%) operators. The quotient must be truncated toward zero. If the result would overflow the 32-bit signed integer range, return 2^31 - 1.

Constraints

  • -2^31 <= dividend <= 2^31 - 1
  • -2^31 <= divisor <= 2^31 - 1
  • divisor != 0
  • Do not use multiplication (*), division (/), or modulo (%) operators
  • The algorithm should be better than O(|dividend|)

Examples

Input: (10, 3)

Expected Output: 3

Explanation: 10 divided by 3 is 3.333..., which truncates toward zero to 3.

Input: (7, -3)

Expected Output: -2

Explanation: 7 divided by -3 is -2.333..., which truncates toward zero to -2.

Input: (-15, 4)

Expected Output: -3

Explanation: -15 divided by 4 is -3.75, which truncates toward zero to -3.

Input: (0, 5)

Expected Output: 0

Explanation: Zero divided by any nonzero integer is 0.

Input: (-2147483648, -1)

Expected Output: 2147483647

Explanation: The mathematical result is 2147483648, which exceeds the maximum 32-bit signed integer, so the result is clamped to 2147483647.

Input: (-2147483648, 1)

Expected Output: -2147483648

Explanation: The quotient is exactly the minimum 32-bit signed integer, so it is returned unchanged.

Hints

  1. Instead of subtracting the divisor one time at a time, try subtracting the largest doubled version of the divisor that fits into the remaining dividend.
  2. Bit shifts can be used to efficiently double numbers and build the quotient from powers of two.
Last updated: Jun 14, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Path Length Through a Grid With One Allowed Cell Conversion - Amazon (medium)
  • Circular Drone Hub Delivery Route - Amazon (hard)
  • Leaf Domain Cumulative Scores - Amazon (medium)
  • Kth Largest Perfect Binary Subtree - Amazon (medium)
  • Find Conflicting Events - Amazon (medium)