Sum Two Large Numbers Represented as Strings
StringsAuto-graded · 19 test casesLast updated
Tests whether a candidate can implement column-by-column addition with carry logic on digit strings, without converting the whole string to a native integer.
Problem Statement
You are given two non-negative integers, each stored as a string of decimal digits. Return their sum, also as a string.
There is one restriction: you may not convert an entire input string into a Python integer (so int(num1) + int(num2) is off limits). You must perform the addition yourself, one digit at a time.
How to approach it
Think of how you would add two numbers on paper: align the rightmost digits, add one column at a time from right to left, record the ones digit of each column, and carry the tens digit into the next column.
Constraints
1 <= len(num1), len(num2) <= 10,000- Both strings contain only the characters
'0'through'9'. - Neither string has leading zeros, except for the single-character string
"0". - The returned string must also have no leading zeros (except for the result
"0"itself).
Examples
Example 1
Input: sum_digit_strings('11', '123')
Output: '134'
Example 2
Input: sum_digit_strings('456', '77')
Output: '533'
Example 3
Input: sum_digit_strings('0', '0')
Output: '0'
Hints
Hint 1
Work from right to left using two index pointers, one for each string. When a pointer goes out of bounds, treat that string as contributing 0 to the current column.
Hint 2
Maintain a carry variable that starts at 0. Each column produces a digit equal to (digit_a + digit_b + carry) % 10 and a new carry equal to (digit_a + digit_b + carry) // 10. The carry can never exceed 1.
Hint 3
Keep the loop running while either pointer is still in range OR the carry is non-zero — this handles the overflow case where the result has more digits than either input (e.g. '99' + '1' = '100'). Collect digits into a list and reverse once at the end instead of prepending on every step.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core idea: right-to-left column addition
This is the same algorithm taught in grade school. Align both numbers at their rightmost digit and process one column at a time moving left. Each column produces one output digit and a carry (0 or 1) that feeds into the next column.
Two index variables, i and j, start at the last position of each string. On every iteration:
- Start
totalat the current carry. - If
iis still in bounds, convertnum1[i]to an integer usingord(c) - ord('0')— a single-digit conversion that respects the no-int()rule — add it tototal, and decrementi. - Do the same for
jandnum2. - Append
total % 10(as a character) to a result list, and setcarry = total // 10.
The loop condition is while i >= 0 or j >= 0 or carry. The or carry part is what handles overflow: when both pointers have run out but a carry of 1 remains (e.g. "99" + "1"), the loop executes one more time and writes the leading 1.
Because digits are produced least-significant-first, reverse the list once at the end and join it into a string.
Why this is correct
The invariant holds throughout: after processing k columns, the list contains the k lowest-order digits of the true sum in reverse, and carry holds the amount to add into column k. A string that has been exhausted contributes 0, which is exactly what the true sum requires (the shorter number has implicit leading zeros). The maximum column total is 9 + 9 + 1 = 19, so the carry is always 0 or 1 and every appended character is a valid single digit.
The loop terminates because both pointers strictly decrease and the carry is bounded at 1, so it can only extend the loop by one extra iteration.
No leading zeros appear in the output because the only extra iteration possible (after both pointers are exhausted) writes a 1, not a 0.
Edge cases
| Input | What happens |
|---|---|
"0" + "0" | Single iteration: total 0, writes "0", carry stays 0, loop ends. |
"99" + "1" | Columns: 10 → write 0, carry 1; 10 → write 0, carry 1; carry-only pass → write 1. Result: "100". |
| Very unequal lengths | The shorter string runs out early; its pointer simply stops contributing. The longer string's remaining digits pass through with only the carry added. |
Complexity
- Time: O(max(m, n)) — one O(1) iteration per column, plus a linear reverse and join at the end.
- Space: O(max(m, n)) — for the output digit list. All other state (
i,j,carry,total) is O(1).
Building the result with a list and a single reversed call matters here. The alternative of prepending each new digit (result = digit + result) copies the whole string on every step and blows up to O(n²) at the 10,000-digit limit.
Full solution
from typing import List
def sum_digit_strings(num1: str, num2: str) -> str:
i, j = len(num1) - 1, len(num2) - 1
carry = 0
digits = []
while i >= 0 or j >= 0 or carry:
total = carry
if i >= 0:
total += ord(num1[i]) - ord('0')
i -= 1
if j >= 0:
total += ord(num2[j]) - ord('0')
j -= 1
digits.append(chr(ord('0') + total % 10))
carry = total // 10
return ''.join(reversed(digits))