PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in bitwise arithmetic, integer overflow semantics, sign handling, and algorithmic efficiency for implementing multiplication without using multiplication or division operators.

  • Medium
  • Other
  • Coding & Algorithms
  • Data Scientist

Implement multiplication without using the multiplication operator

Company: Other

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement int multiply(int a, int b) without using * or /. You may use +, −, bitwise operators, and shifts. Requirements: - Handle negatives, zero, and the corner case INT_MIN * −1. - Achieve O(log |b|) additions via shift‑and‑add (Russian peasant), not naive O(|b|) repeated addition. - Assume 32‑bit signed ints. First implement unchecked overflow (two’s‑complement wrap), then a safe version that saturates to INT_MAX/INT_MIN on overflow without using 64‑bit types. - Analyze time and space complexity and prove correctness for all sign combinations. - Provide unit tests: 0×x, 1×x, −1×x, powers of two, random pairs, INT_MAX×2, INT_MIN×2, INT_MIN×−1. Follow‑ups: how would you extend to big integers and compare with Karatsuba? What changes if the platform lacks arithmetic right shift?

Quick Answer: This question evaluates proficiency in bitwise arithmetic, integer overflow semantics, sign handling, and algorithmic efficiency for implementing multiplication without using multiplication or division operators.

Unchecked 32-bit Multiply Without *

Return a*b using shift-and-add with two-complement 32-bit wraparound.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: (3, 5)

Expected Output: 15

Explanation: Positive values.

Input: (-7, 6)

Expected Output: -42

Explanation: Negative sign.

Input: (2147483647, 2)

Expected Output: -2

Explanation: Wrap overflow.

Input: (0, -99)

Expected Output: 0

Explanation: Zero.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.

Saturating 32-bit Multiply Without *

Return a*b using shift-and-add, saturating to INT_MAX or INT_MIN on overflow.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: (3, 5)

Expected Output: 15

Explanation: Positive values.

Input: (-7, 6)

Expected Output: -42

Explanation: Negative sign.

Input: (2147483647, 2)

Expected Output: 2147483647

Explanation: Positive overflow.

Input: (-2147483648, -1)

Expected Output: 2147483647

Explanation: INT_MIN times -1 saturates.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 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
  • 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

  • Implement a multi-button click detector - Other (hard)
  • Return the k-th row of Pascal-like triangle - Other (medium)
  • Compute total after discounting most expensive item - Other (medium)
  • Prove reservoir sampling correctness - Other (Medium)
  • Write mini-batch gradient descent - Other (Medium)