Skip to main content

46. Multiply Strings

mediumAsked at Snowflake

Multiply two non-negative integers represented as strings, without converting to integer types directly. Snowflake uses this as the canonical fixed-point multiplication exercise — directly relevant to NUMBER(p,s) arithmetic kernels.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake numerics team uses this in onsites to anchor decimal arithmetic discussion.
  • LeetCode Discuss (2025-10)Reported at Snowflake SDE-I screens.

Problem

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Constraints

  • 1 <= num1.length, num2.length <= 200
  • num1 and num2 consist of digits only.
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.

Examples

Example 1

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

Example 2

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

Approaches

1. Repeated addition

Add num1 to itself num2 times. Trivially O(num2 * num1.length) which is up to 10^200 iterations.

Time
ridiculous
Space
O(n)
// Don't ship.
// Mention only as the worst case.

Tradeoff: Worst possible solution.

2. Schoolbook multiplication into digit array (optimal)

Create an array of zeros size m+n. For each digit pair (i, j), accumulate the product into result[i+j+1] with carry into result[i+j]. Convert back to string, trimming leading zeros.

Time
O(m * n)
Space
O(m + n)
function multiply(num1, num2) {
  if (num1 === '0' || num2 === '0') return '0';
  const m = num1.length, n = num2.length;
  const result = new Array(m + n).fill(0);
  for (let i = m - 1; i >= 0; i--) {
    for (let j = n - 1; j >= 0; j--) {
      const a = num1.charCodeAt(i) - 48;
      const b = num2.charCodeAt(j) - 48;
      const sum = a * b + result[i + j + 1];
      result[i + j + 1] = sum % 10;
      result[i + j] += Math.floor(sum / 10);
    }
  }
  return result.join('').replace(/^0+/, '') || '0';
}

Tradeoff: Exactly the long-multiplication algorithm everyone learned in grade school. Karatsuba and FFT can do better asymptotically but aren't expected in an interview.

Snowflake-specific tips

Snowflake interviewers want the m+n digit positioning explained: digit i of num1 times digit j of num2 contributes to position i+j (high) and i+j+1 (low). Bonus signal: discuss when their NUMBER kernel falls back to a software big-int (when intermediate products exceed 64-bit limbs) and how Karatsuba would help for very wide types.

Common mistakes

  • Forgetting the trim of leading zeros — '00' instead of '0'.
  • Off-by-one positioning result[i+j] vs result[i+j+1].
  • Trying to use Number.parseInt or BigInt — explicitly disallowed.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Karatsuba multiplication (O(n^log2(3))).
  • Add Strings (LC 415) — the addition primitive.
  • How does NUMBER(38,2) arithmetic handle overflow internally?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why result has size m+n?

The product of an m-digit and n-digit number has at most m+n digits. Allocating exactly that size avoids resizing.

Why split into i+j and i+j+1?

A digit * digit is at most 81 < 100. The product contributes a low digit (i+j+1) and a high carry (i+j). Splitting makes the carry propagation natural.

Practice these live with InterviewChamp.AI

Drill Multiply Strings and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →