Sum Two Large Numbers Represented as Digit Strings
StringsAuto-graded · 22 test casesLast updated
Tests manual digit-by-digit addition with carry propagation on arbitrarily large integers stored as strings.
Write a function that computes the sum of two non-negative integers represented as strings of decimal digits. Return the result as a string.
The integers may be extremely large—up to 10,000 digits each—far exceeding the capacity of standard integer types. You must implement the addition logic manually by processing individual digits. You may not convert an entire input string to a built-in numeric type, and you may not use external big-integer libraries.
Function Signature
def sum_large_numbers(a: str, b: str) -> str:
Rules
- Each string consists solely of digit characters (
'0'through'9'). - Neither input has unnecessary leading zeros, with the exception that the value zero is represented as
"0". - The two strings may differ in length.
- Your output must be a valid decimal representation with no leading zeros. The only exception is when the sum is zero, which should be returned as
"0".
Constraints
1 <= len(a) <= 10^41 <= len(b) <= 10^4- Each character in
aandbis a digit from'0'to'9'.
Examples
Example 1
Input: sum_large_numbers('11', '123')
Output: '134'
Example 2
Input: sum_large_numbers('999', '1')
Output: '1000'
Example 3
Input: sum_large_numbers('0', '0')
Output: '0'
Hints
Hint 1
Process digits from right to left, just like manual addition on paper, keeping track of the carry at each position.
Hint 2
Handle strings of different lengths by using separate indices for each input, treating exhausted positions as contributing zero.
Hint 3
Don't forget to handle the final carry after both strings are fully processed—it may add an extra leading digit to the result.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires implementing elementary-school addition: align the numbers by their rightmost digits, add corresponding digits column by column from right to left, and propagate any carry to the next position.
We maintain two pointers starting at the last character of each string and move leftward. At each step, we extract the current digit from each string (if the pointer is still valid), add them together along with any carry from the previous column, emit the ones place of that sum as the next output digit, and save the tens place as the new carry.
The algorithm continues until both pointers have moved past the beginning of their strings and no carry remains. This handles the case where the final carry generates an additional leading digit (e.g., "999" + "1" = "1000").
Since we generate digits from least significant to most significant, we collect them in a list and reverse once at the end. This avoids the quadratic cost of repeatedly prepending to a string.
Why It Works
Each column computes the sum of at most three single-digit values: one digit from each input (0 if that input is exhausted) plus the carry from the previous column (0 or 1). The maximum sum is 9 + 9 + 1 = 19, so each column produces a single output digit (sum mod 10) and a carry of at most 1 (sum div 10).
By processing every column and ensuring the loop only terminates when the carry is zero, we guarantee the output represents the exact sum with no lost information. The output naturally has no leading zeros because:
- If both inputs are
"0", we emit exactly"0". - If the most significant digit comes from a carry, it's always
1. - Otherwise, it comes from the leading digit of one of the inputs, which is never
0by the input guarantee.
Complexity
- Time: O(max(n, m)), where n and m are the lengths of the two input strings. We process each digit exactly once.
- Space: O(max(n, m)) for the output list. The result has at most max(n, m) + 1 digits, which is unavoidable since we must return the answer.
Solution Code
def sum_large_numbers(a, b):
i, j = len(a) - 1, len(b) - 1
carry = 0
result = []
while i >= 0 or j >= 0 or carry:
digit_sum = carry
if i >= 0:
digit_sum += ord(a[i]) - ord('0')
i -= 1
if j >= 0:
digit_sum += ord(b[j]) - ord('0')
j -= 1
result.append(chr(ord('0') + digit_sum % 10))
carry = digit_sum // 10
return ''.join(reversed(result))