Skip to main content

46. Multiply Strings

mediumAsked at Reddit

Multiply two big integers represented as strings. Reddit asks this to test grade-school multiplication translated to code — the same shape used in their cross-shard counter merging where each shard returns a digit-encoded partial sum.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen for senior backend roles.

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. Add num1 * digit chunks

For each digit of num2, multiply num1 by that digit, shift, then string-add.

Time
O((m + n) * m)
Space
O(m + n)
// Multiple add steps; each add is O(m+n). Total roughly O(m*(m+n)).

Tradeoff: Works but creates many intermediate strings.

2. Position-indexed product array (optimal)

Allocate result array of size m + n. Multiply digit pairs into positions (i + j) and (i + j + 1). Propagate carries at the end.

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 pos = new Array(m + n).fill(0);
  for (let i = m - 1; i >= 0; i--) {
    for (let j = n - 1; j >= 0; j--) {
      const product = Number(num1[i]) * Number(num2[j]);
      const sum = pos[i + j + 1] + product;
      pos[i + j + 1] = sum % 10;
      pos[i + j] += Math.floor(sum / 10);
    }
  }
  while (pos.length > 1 && pos[0] === 0) pos.shift();
  return pos.join('');
}

Tradeoff: Tight inner loop, deferred carry propagation. The (i+j, i+j+1) indexing is the canonical answer.

Reddit-specific tips

Reddit interviewers want the (i+j, i+j+1) positional trick. Bonus signal: walk through 12 * 34 by hand showing the carry positions. Mention that for very large numbers, Karatsuba's O(n^1.58) is theoretically better.

Common mistakes

  • Early-zero check forgotten — '0' * anything must return '0'.
  • Forgetting to trim leading zeros.
  • Using parseInt for large strings.

Follow-up questions

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

  • Add strings (LC 415) — simpler.
  • Karatsuba multiplication — divide and conquer.
  • Power of two large numbers.

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 position (i + j + 1) for the units digit?

Multiplying num1[i] * num2[j] gives a two-digit product. The units digit goes to i+j+1; the tens digit to i+j (higher place value).

Could we use BigInt?

Yes — one-liner. But the problem disallows it; the educational point is the grade-school algorithm.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →