Leetcode 415. Add Strings
Asked at:
Meta
Uber
DESCRIPTION
Given two non-negative integers represented as decimal strings, return their sum as a string by simulating digit-by-digit addition from right to left with carry propagation. You cannot convert the entire strings to integers or use big-integer libraries. For example, adding "123" and "456" should return "579" by processing digits 3+6=9, 2+5=7, 1+4=5.
Input:
num1 = "123" num2 = "456"
Output:
"579"
Explanation: Add digit-by-digit: 3+6=9, 2+5=7, 1+4=5
Constraints:
- String lengths up to 10^4 (must handle arbitrary-length inputs)
- Strings contain only digits 0-9
- No leading zeros except for the number 0 itself
- Cannot convert entire strings to integers or use big-integer libraries
Understanding the Problem
The challenge is simulating elementary school addition programmatically: process digits from least significant (rightmost) to most significant (leftmost), maintaining a carry that propagates forward. Strings may have different lengths, requiring careful index management. The result must be built efficiently without unnecessary string concatenations, and a final carry may add an extra digit.
Building Intuition
A naive approach might try converting strings to integers, but this fails for large inputs exceeding integer limits. The optimal approach mimics manual addition: iterate from the rightmost digits, sum corresponding digits plus any carry, store the result digit, and propagate the carry forward. For example, adding "99" and "1": start with 9+1=10 (write 0, carry 1), then 9+0+1=10 (write 0, carry 1), finally write the remaining carry 1 to get "100".
This technique is fundamental for arbitrary-precision arithmetic used in cryptography, financial calculations, and scientific computing where numbers exceed native integer sizes. Understanding carry propagation and string manipulation without type conversion is essential for low-level numeric processing.
Common Pitfalls
Implementation
Digit-by-Digit Addition with Carry
Implement the core addition loop that processes digits from right to left using two pointers (one for each string). At each step, extract digits at current positions (or 0 if out of bounds), compute sum = digit1 + digit2 + carry, store sum % 10 as the result digit, and update carry = sum // 10. Continue until both strings are exhausted and no carry remains. For example, adding "456" and "77": process 6+7=13 (write 3, carry 1), 5+7+1=13 (write 3, carry 1), 4+0+1=5 (write 5), yielding "533".
def add_strings(num1: str, num2: str) -> str:i, j = len(num1) - 1, len(num2) - 1carry = 0result = []while i >= 0 or j >= 0 or carry:digit1 = int(num1[i]) if i >= 0 else 0digit2 = int(num2[j]) if j >= 0 else 0total = digit1 + digit2 + carryresult.append(str(total % 10))carry = total // 10i -= 1j -= 1return ''.join(reversed(result))
Efficient Result Construction
Build the result efficiently by appending digits to a list/array during the addition loop (since we process right-to-left but build left-to-right in memory). After the loop completes, reverse the list to get the correct digit order, then join into a string. This avoids O(n²) string concatenation overhead. For example, processing "99"+"1" appends [0, 0, 1] to the list, then reverses to [1, 0, 0] and joins to "100".
# Efficient Result Construction is already implemented in Section 1# The add_strings function uses:# 1. result = [] - list to collect digits# 2. result.append(str(total % 10)) - append each digit# 3. ''.join(reversed(result)) - reverse and join# Example usage of the existing function:# add_strings("99", "1") returns "100"# Internally: result=[0,0,1] -> reversed=[1,0,0] -> "100"
What We've Learned
- Pattern: Simulate manual arithmetic with two-pointer traversal from least to most significant digits, maintaining carry state
- Use Case: Foundation for arbitrary-precision arithmetic in cryptography, financial systems, and any domain requiring calculations beyond native integer limits
- Optimization: Use array/list building with final reversal instead of string concatenation to achieve O(n) time complexity
- Edge Handling: Always check for remaining carry after main loop and handle different-length inputs gracefully with implicit zero-padding
Problems to Practice
easy
This problem requires comparing values from both ends of a data structure moving inward, similar to how Add Strings processes digits from right to left. Both problems involve careful pointer/index management and handling elements in reverse order without converting to native types.
medium
Like Add Strings, this problem involves building a result string character-by-character through careful state management and processing characters in a specific order. Both require handling nested operations (carry propagation vs nested brackets) and constructing output strings incrementally.
medium
Both problems involve careful string traversal with index management and character-level processing. Add Strings uses two pointers moving from right to left with carry state, while this problem uses a sliding window approach, teaching complementary string manipulation techniques.
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Early November, 2025
Meta
Senior
Early October, 2025
Meta
Senior
Late September, 2025
Uber
Senior
Exact leetcode question
Comments
Hello Interview Premium
Your account is free and you can post anonymously if you choose.