Multiply Strings

Multiply Strings is the big sibling of Add Strings. The same trap is here. You cannot just convert to numbers, because the numbers can be huge. So you multiply them by hand, like you did in school. This one trips people up because of where each digit lands. Get the position right and the rest is easy.

🎯 The Problem

You get two numbers as strings and return their product as a string. The whole trick is position, where each digit pair lands in the answer.

The rules:

  • The inputs are non-negative numbers given as strings.
  • You may not convert the whole string to an integer.
  • You multiply digit by digit, like you did in school.
  • Digit i of the first number times digit j of the second lands at slots i + j and i + j + 1.
Input: num1 = "12", num2 = "24"
Output: "288"
Explanation:
1 2
x 2 4
-----
4 8 (12 x 4)
2 4 (12 x 20)
-----
2 8 8

The product of two numbers with m and n digits has at most m + n digits. So we make an answer array of that size and fill it.

Here is the position rule. Each pair of digits from the two numbers feeds two slots in the result array.

num1 digit at i

product = d1 x d2

num2 digit at j

low part goes to slot i+j+1

high part carries to slot i+j

Add into answer array

Read array, skip leading zeros

🐒 Approach 1: Convert And Multiply (Brute Force)

The idea in one line: turn both strings into numbers, multiply, then turn the result back to a string.

The idea:

  • Call int(num1) * int(num2).
  • Convert the product back into a string.

How it works:

  • The language parses each string into an integer.
  • The built-in multiply does the work.

Why it is weak:

  • The product of two long numbers is even longer.
  • In C, C++, and Java it overflows. Overflow means the value grew past what the type can store, so it wraps to a wrong number.
  • Python handles big integers fine, but the interviewer wants the manual method.

Here is the conversion code for that idea:

multiply_strings_conversion.py
def multiply(num1, num2):
return str(int(num1) * int(num2))

⚑ Approach 2: Grade-School With A Position Grid (Best)

The idea in one line: multiply digit by digit and drop each product into fixed slots of an answer array.

The idea:

  • Make an answer array of size m + n, filled with zeros.
  • Multiply each digit of num1 by each digit of num2.
  • Each digit pair feeds two fixed slots.

How it works:

  • For the pair at indices i and j, the product is 0 to 81.
  • Slot p2 = i + j + 1 holds the low place. Slot p1 = i + j holds the carry place.
  • Add the product to whatever sits at p2. The last digit stays at p2. The rest carries up to p1.
  • Read the array left to right and skip leading zeros. If everything is zero, return "0".

Why it is fast:

  • The position rule means each product always lands at i + j and i + j + 1.
  • Two nested loops touch each digit pair once, so time is O(m times n).
  • It never converts the whole string, so it works for any length.

Steps to Solve

  1. If either string is "0", the answer is "0". Handle that first.
  2. Make an answer array of size m + n, all zeros.
  3. Loop i over num1 from the last digit to the first.
  4. Inside, loop j over num2 from the last digit to the first.
  5. Multiply the two digits. Add the product to the value already at position i + j + 1.
  6. The digit kept at i + j + 1 is that sum modulo ten. The carry added to position i + j is the sum divided by ten.
  7. After both loops, read the array left to right, skip leading zeros, and join into a string.

Here is the answer array for "12" times "24". Each digit pair drops into its slot, and the carries settle to give 288.

Answer slots start at 0 0 0 0

2 x 4 = 8 into slot 3

1 x 4 = 4 into slot 2

2 x 2 = 4 into slot 2, now 8

1 x 2 = 2 into slot 1

Slots 0 2 8 8, skip leading 0

Result = 288

This Python version uses a list for the answer slots, then joins the digits and strips leading zeros.

multiply_strings.py
def multiply(num1, num2):
if num1 == "0" or num2 == "0":
return "0"
m, n = len(num1), len(num2)
pos = [0] * (m + n) # answer slots
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
mul = (ord(num1[i]) - 48) * (ord(num2[j]) - 48)
p1, p2 = i + j, i + j + 1 # carry slot, low slot
total = mul + pos[p2]
pos[p2] = total % 10 # keep last digit
pos[p1] += total // 10 # push carry left
result = "".join(str(d) for d in pos).lstrip("0") # drop leading zeros
return result if result else "0"
print(multiply("12", "24"))

The output of the above code will be:

288

Let us read the Python version line by line. First we check for "0". If either input is zero, the product is "0", so we return early. This avoids a messy answer full of zeros.

Then pos = [0] * (m + n) makes the answer array. Two numbers with m and n digits multiply into at most m + n digits, so this size always fits. Every slot starts at zero.

The outer loop runs i from the last digit of num1 back to the first. The inner loop runs j from the last digit of num2 back to the first. So every digit of the first number meets every digit of the second.

Inside, mul = (ord(num1[i]) - 48) * (ord(num2[j]) - 48) is the product of the two single digits. The ord(...) - 48 turns a character like '7' into the number 7. Then p1, p2 = i + j, i + j + 1 are the two slots this product touches. We add mul to whatever is already at p2. The last digit of that sum stays at p2. The rest carries to p1 with +=, because that slot may already hold a value from an earlier pair.

At the end we join all slots into one string and call lstrip("0") to remove leading zeros. If everything was stripped, we fall back to "0".

⏱️ Time and Space Complexity

We have two nested loops, one over each string. So the time is O(m times n), where m and n are the two lengths. The answer array has size m + n, so the space is O(m + n). There is a faster method using Fast Fourier Transform for very large numbers, but it is far more complex and almost never expected in an interview.

Approach Time Complexity Space Complexity
Convert to int and multiply Overflows on large input O(m + n)
Grade-school with position grid O(m Γ— n) O(m + n)

Tip

The position rule is the heart of this problem. The product of digit i and digit j always lands at slots i + j and i + j + 1. Memorize that and the code writes itself.

🧩 Key Takeaways

  • βœ… The product of two numbers with m and n digits has at most m + n digits, so size your array that way.
  • βœ… Digit i times digit j lands at slots i + j and i + j + 1 of the answer.
  • βœ… Add the product into the low slot, keep the last digit, and carry the rest left.
  • βœ… Handle the zero case first, or you get a string of zeros instead of β€œ0”.
  • βœ… Skip leading zeros when you read the array into the final string.

Check Your Knowledge

4 questions Show quiz Hide quiz

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    How many digits can the product of an m-digit and an n-digit number have at most?

    Why: The product has at most m + n digits, so the answer array is sized m + n.

  2. 2

    When you multiply digit i of num1 by digit j of num2, which slots does it affect?

    Why: Each digit product feeds the low slot i + j + 1 and carries into slot i + j.

  3. 3

    Why do you handle the case where either input is "0" first?

    Why: Without the early check you would build something like "00", so returning "0" up front is cleaner.

  4. 4

    What is the time complexity of the grade-school string multiplication?

    Why: Two nested loops, one over each string, give O(m Γ— n) time.

πŸš€ What’s Next?