Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Math & Geometry

Multiply Strings

MEDIUM✓ CHIP-TIMEDLC #43 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Multiply two non-negative integers given as digit strings and hand back their product, also as a string — the numbers may be too long for a built-in integer type to hold along the way.

Two non-negative integers arrive as strings of digits, potentially far too long to fit into any built-in integer type, and the task is to multiply them together.

The product has to be computed using only string and digit-level arithmetic — no shortcut through a native numeric type that could silently overflow or lose precision on inputs this large.

The result comes back as a string too, holding the exact product with no leading zeros, unless the true product is zero itself, in which case a single "0" is the right answer.

  • each digit string runs up to a couple hundred characters
  • both inputs are non-negative integers with no leading zeros, other than "0" itself
  • the product must be built without casting either input to a native integer
  • the returned string carries no leading zeros except for a lone "0"
EX 01
num1 = "2" · num2 = "3"
→ "6"
single digits, no carry
EX 02
num1 = "0" · num2 = "0"
→ "0"
zero times zero
EX 03
num1 = "0" · num2 = "582"
→ "0"
zero short-circuits regardless of the other operand

The hintstake only what you need

HINT 1 THE NUDGE

Casting straight to a native integer dodges the actual exercise — for numbers that could run hundreds of digits long, the arithmetic has to live entirely on the strings. How did you multiply big numbers on paper, before a calculator?

HINT 2 THE STRUCTURE

Every digit of the first number times every digit of the second contributes to exactly one place value. The digit at position i from the right in num1 and position j from the right in num2 always lands at position i+j (and possibly carries into i+j+1) — no matter what the rest of the numbers look like.

HINT 3 ONE STEP FROM THE ANSWER

Multiply into a result array sized len(num1)+len(num2): for each digit pair, add the product into position i+j and let the carry spill into i+j+1. One final pass resolves leftover carries and trims a possible leading zero.

Coach's boardthe pattern, step by step

THE DIGIT CONVOLUTIONPATTERN · DIGIT CONVOLUTIONnum1 = "12" · num2 = "34"
0
0
0
0
STEP 1

12 × 34: allocate a result array sized len(num1)+len(num2) = 4, all zero. Every digit pair has one fixed destination.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/multiply-strings.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        if num1 == "0" or num2 == "0":
            return "0"

        n, m = len(num1), len(num2)
        result = [0] * (n + m)  # result[i+j] and result[i+j+1] absorb every digit pair

        for i in range(n - 1, -1, -1):
            for j in range(m - 1, -1, -1):
                mul = int(num1[i]) * int(num2[j])
                p1, p2 = i + j, i + j + 1
                total = mul + result[p2]
                result[p2] = total % 10
                result[p1] += total // 10

        start = 0
        while start < len(result) - 1 and result[start] == 0:
            start += 1
        return "".join(map(str, result[start:]))
TIME O(N·M)SPACE O(N+M)PYTHON · RACE PACE · 20 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED