Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Add Strings

Add Strings

Add two non-negative integers given as decimal strings without converting them to numbers. A test of grade-school addition with a carry and two pointers from the right. Solve it below in Python or JavaScript, then read the walkthrough.

Solve it in your browserRead the solution
EasyMathStringSimulationAsked at 87 companies incl. Google, Amazon, Meta

Problem

You're given two non-negative integers num1 and num2, each written as a string of decimal digits. Return their sum, also as a string of digits.

The numbers can be far longer than any built-in integer type holds, so you may not convert the whole strings to numbers (no int(num1), BigInt, or similar). Work digit by digit.

Neither input has leading zeros unless the number is exactly "0", and your answer shouldn't have leading zeros either.

Example 1

Input
num1 = "58", num2 = "467"
Output
"525"

8 + 7 = 15, write 5 carry 1. 5 + 6 + 1 = 12, write 2 carry 1. 4 + 1 = 5.

Example 2

Input
num1 = "999", num2 = "1"
Output
"1000"

The carry ripples through every digit and adds a new leading digit.

Example 3

Input
num1 = "0", num2 = "306"
Output
"306"

Constraints

  • 1 ≤ num1.length, num2.length ≤ 10⁴
  • num1 and num2 contain only the digits 0 to 9.
  • Neither input has leading zeros, except the number "0" itself.

Practice

1
2
3
4

Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Add Strings in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Add Strings

Pattern: Digit-by-digit addition with a carry

This is the column addition you learned in school. Line the numbers up on the right, add the two digits in each column plus the carry from the previous column, write down sum % 10, and carry sum // 10 (which is always 0 or 1) to the next column.

Two pointers, one at the end of each string, walk left together. When one string runs out, treat its missing digits as 0. Keep going while either pointer has digits left or a carry remains, so "999" + "1" gets its extra leading 1.

Digits come out from least to most significant, so collect them in a list and reverse once at the end instead of prepending to a string each step.

Approach 1: Convert to numbers and add

Parse both strings, add, and format the result. It is the obvious shortcut, but it is not allowed here, and with fixed-size integers it silently overflows past about 19 digits (or loses precision past 15 to 16 digits in JavaScript).

Time O(n) where the language supports big integers · Space O(n)

Approach 2: Pad and add, prepending each digit

Left-pad the shorter string with zeros, then add column by column from the right and prepend each result digit to the answer string. Correct, but prepending to an immutable string copies it every step.

Time O(n²) because of the repeated string copies · Space O(n)

Approach 3: Two pointers from the right with a carry

  1. Set i and j to the last index of each string and carry = 0.
  2. While i ≥ 0, j ≥ 0, or carry > 0: add the digit at i (or 0), the digit at j (or 0), and carry. Append total % 10 to a list, set carry = total // 10, and move both pointers left.
  3. Reverse the list and join it.

Each digit is read once and each output digit is appended once.

Time O(max(m, n)) · Space O(max(m, n)) for the output

ApproachTimeSpace
Convert to numbers and addO(n) where the language supports big integersO(n)
Pad and add, prepending each digitO(n²) because of the repeated string copiesO(n)
Two pointers from the right with a carryO(max(m, n))O(max(m, n)) for the output

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Add Strings solution in Python
def add_strings(num1: str, num2: str) -> str:
    i, j, carry = len(num1) - 1, len(num2) - 1, 0
    digits: list[str] = []
    while i >= 0 or j >= 0 or carry:
        total = carry
        if i >= 0:
            total += ord(num1[i]) - 48
            i -= 1
        if j >= 0:
            total += ord(num2[j]) - 48
            j -= 1
        digits.append(chr(48 + total % 10))
        carry = total // 10
    return "".join(reversed(digits))
▸Add Strings solution in JavaScript
function addStrings(num1, num2) {
  let i = num1.length - 1;
  let j = num2.length - 1;
  let carry = 0;
  const digits = [];
  while (i >= 0 || j >= 0 || carry > 0) {
    let total = carry;
    if (i >= 0) total += num1.charCodeAt(i--) - 48;
    if (j >= 0) total += num2.charCodeAt(j--) - 48;
    digits.push(total % 10);
    carry = Math.floor(total / 10);
  }
  return digits.reverse().join('');
}

Common mistakes

  • Stopping when both strings are exhausted and dropping a final carry, so "999" + "1" becomes "000".
  • Building the answer by prepending to a string inside the loop, which makes the whole thing quadratic.
  • Using the character itself instead of its digit value. Subtract the code of "0" (or use int(ch) on one character).
  • Producing leading zeros, for example by padding both inputs and never trimming. With the carry-driven loop above this cannot happen.

What interviewers look for

Confirm up front that converting the whole string is off limits; it is the entire point of the question. Then describe the loop condition in one sentence: "keep going while either number has digits or there is a carry." That single condition handles unequal lengths and the final carry, and saying it shows you have thought about both.

Mention why you append and reverse rather than prepend. Interviewers like to hear that you know string concatenation in a loop can be quadratic.

Follow-up questions

How would you multiply two numbers given as strings?

Allocate an array of length m + n. For every pair of digits at positions i and j (from the right), add their product into slot i + j, then do one pass to push carries forward. That is O(m · n) and avoids big-integer conversion, the same way long multiplication works on paper.

What if the numbers are in base 2, as in Add Binary?

The loop is identical with 10 replaced by 2: write total % 2 and carry total // 2. Any base b works the same way.

What if the inputs could be negative?

Handle the signs first. If the signs match, add the magnitudes and keep the sign. If they differ, compare the magnitudes, subtract the smaller from the larger digit by digit with a borrow, and give the result the sign of the larger one, trimming leading zeros.

Companies that ask Add Strings

Add Strings is tagged by 87 companies in our interview dataset. The score shows how often it comes up at each company.

  • Freelancercom100%
  • Liveperson100%
  • Pluralsight100%
  • SpaceX100%
  • Wepay88%
  • System188%
  • Ceridian87%
  • Bio Rad Laboratories86%
  • GitLab84%
  • Talabat84%
  • Mollie83%
  • UPS82%
  • Ikea82%
  • Xandr81%
  • Align Technology81%
  • Applovin80%
  • Cummins78%
  • Ericsson76%
  • Nexient76%
  • Wayfair72.1%
  • Alphasense71%
  • Slalom69%
  • Wex68%
  • Evolutioniq68%

and 63 more.

Practice next

  • Roman to IntegerEasyCompare each symbol with the next
  • Basic Calculator IIMediumRunning term with deferred addition
  • Basic CalculatorHardRunning sum with a stack of saved contexts
  • Fraction to Recurring DecimalMediumLong division with a remainder map

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.