Problem Statement

Multiply Strings

You get two numbers, num1 and num2, but they come to you as strings of digit characters like "123", not as actual numbers. Your job is to multiply them and return the answer as a string too. The catch: you are not allowed to turn the whole string into a big number and multiply, and you cannot use a special big-number library. So we do it the way you learned in grade school, multiplying digit by digit and adding things up by hand. The one trick to remember: when you multiply the digit at position i of num1 by the digit at position j of num2, that little product lands in positions i+j and i+j+1 of an answer array. We add up all these little products into the array, carry the tens when a spot goes over 9, and finally glue the digits into a string.

mediumStringMathMath & Number TheoryTime: O(n * m) · Space: O(n + m)

Signals to notice

multiply two number stringsdigit-by-digit like paper multiplicationmanage positions and carries

Brute force first

Not applicable — digit multiplication IS the approach.

The key insight

Result array of size m+n. For digits i,j: result[i+j+1] += num1[i] × num2[j]. Propagate carries. Strip leading zeros. O(m × n).

Trace it on num1="123", num2="456"

init: m=3, n=3, result=[0,0,0,0,0,0]
i=2(3),j=2(6): mul=18, p2=5, total=18 -> result[5]=8, result[4]+=1 => [0,0,0,0,1,8]
i=2(3),j=1(5): mul=15, p2=4, total=15+1=16 -> result[4]=6, result[3]+=1 => [0,0,0,1,6,8]
i=2(3),j=0(4): mul=12, p2=3, total=12+1=13 -> result[3]=3, result[2]+=1 => [0,0,1,3,6,8]
i=1(2),j=2(6): mul=12, p2=4, total=12+6=18 -> result[4]=8, result[3]+=1 => [0,0,1,4,8,8]
i=1(2),j=1(5): total=10+4=14 -> result[3]=4, result[2]+=1 => [0,0,2,4,8,8]; j=0(4): total=8+2=10 -> result[2]=0, result[1]+=1 => [0,1,0,4,8,8]
i=0(1),j=2(6): total=6+4=10 -> result[3]=0, result[2]+=1 => [0,1,1,0,8,8]; j=1(5): total=5+1=6 -> result[2]=6 => [0,1,6,0,8,8]; j=0(4): total=4+1=5 -> result[1]=5 => [0,5,6,0,8,8]
join+strip leading 0 => "56088"

What must stay true

Product of digits at positions i and j contributes to result position i+j+1 with carry to i+j — mirrors paper multiplication.

Shape of the loop

result = array of zeros, size m+n
for i from m-1 down to 0:
  for j from n-1 down to 0:
    total = num1[i]*num2[j] + result[i+j+1]
    result[i+j+1] = total % 10
    result[i+j]  += total // 10
return join(result) with leading zeros stripped (or "0" if empty)

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Leading zeros in result — strip them but return '0' if all zeros.

Math & Number Theory Pattern