Skip to content
AI360Xpert
Beta
Difficulty: MediumMath & Geometry

Multiply Strings

Problem in Plain English

Multiply two non-negative integers supplied as decimal strings and return the product as a string, without converting either input to an integer or using a big-integer library.

Problem Statement

Given two non-negative integers num1 and num2 represented as decimal strings, return their product, also represented as a decimal string.

The inputs can hold up to 200 digits, far beyond what a fixed-width integer stores, so the digits must be combined by hand. Do not convert the whole strings to integers and do not use a big-integer library.

Constraints

  • 1≤num1.length,num2.length≤2001 \leq \text{num1.length}, \text{num2.length} \leq 200.
  • num1 and num2 consist of digits only.
  • Neither input has a leading zero, except the number 0 itself.

Examples

Example 1

Input
num1 = "2", num2 = "3"
Output
"6"

The only digit pair contributes 2×3=62 \times 3 = 6 units, so the canonical decimal result is "6".

Example 2

Input
num1 = "123", num2 = "456"
Output
"56088"

The shifted rows are 738, 6150 and 49200, whose sum is 56088. Several columns receive contributions, so their carries must be preserved.

Example 3

Input
num1 = "0", num2 = "999"
Output
"0"

Zero times any non-negative integer is zero. Return exactly "0", without extra leading zeros or an empty string.

Example 4

Input
num1 = "99", num2 = "99"
Output
"9801"

The units row is 891 and the tens row is 8910. Their sum 9801 shows that a carry can propagate across overlapping partial products.

Intuition

Decimal place value lets us multiply arbitrarily long inputs using only small digit arithmetic. In Example 1, the units digits 2 and 3 contribute 6 units. For a longer input, every digit pair contributes at its combined place value; add those contributions and carry each group of ten into the next column. Whole-number conversion is unnecessary.

Approaches

Partial Products

Solution Details

Reveal Partial Products: intuition, complexity, and code

Hints

Hint 1
Do the multiplication on paper for 123 times 456: each digit of 456 produces its own shifted row before anything is added.
Hint 2
A digit at index i of num1 times a digit at index j of num2 always lands on the same answer positions: i + j and i + j + 1.
Hint 3
Use an array of m + n answer slots. For each pair, combine the existing units slot with the product, store its remainder modulo ten, and add its quotient to the slot immediately left.

Edge Cases

  • Either input is "0": return "0" at once instead of building an empty result.
  • Maximum-length inputs of 200 digits: the pairwise loops still run fast, but converting to a number would overflow.
  • Leading zeros in the result array, such as the spare slot when the product is short — strip them before joining.
  • Internal zero digits such as "101" times "20" still need their correct decimal shifts; the result is "2020".
  • Long runs of nines cause overlapping carries: "99" times "99" yields "9801", not separate concatenated digit products.

Common Mistakes and Interview Tips

  • Converting both strings to integers first, which overflows on long inputs and breaks the no-conversion rule.
  • Writing the carry into res[i + j] with plain assignment instead of +=, wiping out carries left by earlier pairs.
  • Forgetting to skip leading zeros, so short products come out with extra 0 digits at the front.
  • Processing the array in an arbitrary order can leave unnormalized carry slots; the shown code relies on both loops moving right to left.
  • Interview tip: derive slot i + j + 1 from decimal weights before coding; explain why the left carry uses addition and can temporarily exceed nine.

Key Takeaway

Represent long decimal arithmetic by small digit contributions and their place values. Either add shifted rows or accumulate in a shared array; carry operations must preserve the represented value at every step.