Browse curriculum

Multiply Strings

Multiply two numbers given as strings without converting them, using grade-school multiplication into an array of m + n digit positions.

Problem Understanding

Multiply Strings: given two non-negative integers num1 and num2 as strings, return their product as a string. You may not use a big-integer library or convert the inputs to integers directly.

Examples: "2" × "3" = "6", and "123" × "456" = "56088".

The Intuition: Every Digit Pair Has a Fixed Place

Long multiplication on paper multiplies every digit of one number by every digit of the other and adds the results in the right columns. Do the same with an array:

  1. The answer has at most m + n digits, so allocate pos of that length.
  2. num1[i] × num2[j] belongs at pos[i + j + 1], with its tens carried to pos[i + j].
  3. Walk both strings from the right, so carries flow left into positions that are visited later.

Two indices sweeping a pair of strings from the end is the same shape as adding two numbers digit by digit, a cousin of the two pointers pattern. Leading zeros only appear at the front, as in Remove K Digits.

Interactive Walkthrough

num1 and num2 sit right-aligned above pos, the way you write long multiplication. Each step multiplies digits i and j; the ones position is solid and the carry position is tinted. Try All nines to see every position used.

Dry Run Table

Input: num1 = "123", num2 = "456", so pos has 6 places.

i, j Digits Product + pos[i+j+1] pos after
2, 2 3 × 6 18 + 0 = 18 0 0 0 0 1 8
2, 1 3 × 5 15 + 1 = 16 0 0 0 1 6 8
2, 0 3 × 4 12 + 1 = 13 0 0 1 3 6 8
1, 2 2 × 6 12 + 6 = 18 0 0 1 4 8 8
1, 1 2 × 5 10 + 4 = 14 0 0 2 4 8 8
1, 0 2 × 4 8 + 2 = 10 0 1 0 4 8 8
0, 2 1 × 6 6 + 4 = 10 0 1 1 0 8 8
0, 1 1 × 5 5 + 1 = 6 0 1 6 0 8 8
0, 0 1 × 4 4 + 1 = 5 0 5 6 0 8 8

Answer: 056088 without its leading zero: "56088".

The Solution Template

One position array, filled from the right. A position can briefly hold more than 9; it is settled when the loop next uses it as the ones place.

Multiply Strings Code
1var multiply = function(num1, num2) {
2 const m = num1.length, n = num2.length;
3 const pos = Array(m + n).fill(0);
4 for (let i = m - 1; i >= 0; i--) {
5 for (let j = n - 1; j >= 0; j--) {
6 const mul = Number(num1[i]) * Number(num2[j]);
7 const total = mul + pos[i + j + 1];
8 pos[i + j + 1] = total % 10;
9 pos[i + j] += Math.floor(total / 10);
10 }
11 }
12 const result = pos.join('').replace(/^0+/, '');
13 return result || '0';
14};

Edge Cases & Common Mistakes

  • A zero input: every position stays 0; return "0", not an empty string.
  • Leading zeros: the product may have m + n - 1 digits; strip the front zero.
  • Off-by-one positions: the ones digit goes to i + j + 1, the carry to i + j.
  • Converting to int or long: overflows for long inputs and is not allowed.
  • Building the result by repeated string addition: correct but slower and far more code than the position array.

Summary

Allocate m + n positions. For every digit pair from the right, add the product to pos[i + j + 1], keep the last digit there and carry the rest to pos[i + j]. Join, strip leading zeros, and return "0" if nothing is left. O(m · n) time, O(m + n) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Allocate m + n positions

    pos = [0] * (m + n).

  2. Multiply every digit pair from the right

    For i from m - 1 down and j from n - 1 down: total = num1[i] · num2[j] + pos[i + j + 1]; pos[i + j + 1] = total % 10; pos[i + j] += total / 10.

  3. Build the string

    Join pos, drop leading zeros, and return "0" if nothing is left.

Frequently asked questions

Why does digit i times digit j go to position i + j + 1?

Index the result array from the left with length m + n. num1[i] is worth 10^(m - 1 - i) and num2[j] is worth 10^(n - 1 - j), so their product is worth 10^(m + n - 2 - i - j). In a left-indexed array of length m + n, that place is index i + j + 1; the carry goes one place left, to i + j.


Why is the result at most m + n digits?

num1 < 10^m and num2 < 10^n, so their product is less than 10^(m + n), which has at most m + n digits. It may have one fewer, which shows up as a leading zero in the array.


Why not convert the strings to integers?

The problem forbids it, and the inputs can be up to 200 digits, far beyond 64-bit integers. Working digit by digit keeps every intermediate value small.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description