◀ THE GRIND — MATH & GEOMETRY

Multiply Strings

MEDIUM✓ CHIP-TIMEDLC #43 — FULL STATEMENT ↗

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.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

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.

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 HINTS — TAKE 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 BOARD — THE 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 SPLITS — TWO PACES, TWO LANGUAGES
grind/multiply-strings.pyRACE 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-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED