Function that Multiples 2 Integers

Write a multiply function that multiples 2 integers without using “*”.

💡Strategies for Solving This Problem

Without Using Multiplication Operator

This showed up at Amazon. The twist: "Don't use *, /, or library functions." It's testing if you understand what multiplication really is.

Approach 1: Repeated Addition

3 × 4 = 3 + 3 + 3 + 3 (add 3 four times)

O(b) time where b is the second number. Works but slow for large numbers.

Approach 2: Bit Manipulation (Russian Peasant)

The clever approach using bit shifts:

  • a × b = a × (b/2 + b/2) if b is even
  • a × b = a + a × (b-1) if b is odd

This is O(log b) time - way better.

Why Bit Manipulation

Think of b in binary: 5 = 101₂ = 4 + 1

So a × 5 = a × 4 + a × 1 = (a << 2) + a

We can decompose any multiplication into additions of bit-shifted values.

Edge Cases

  • Negative numbers (handle sign separately)
  • Zero (return 0 early)
  • Overflow (depends on language)

My interviewer specifically asked about negatives. I forgot to handle them initially - don't make that mistake.

Scroll to Top