Add Two Non-Negative Integers Given as Strings Without Integer Conversion

Quick Overview

Add two non-negative integers that arrive as decimal strings of up to 10,000 digits and return the sum as a string, without converting the inputs to integers or using a big-integer library. It tests digit-by-digit addition with a carry, inputs of different lengths, and building the output in linear time.

Add Two Non-Negative Integers Given as Strings Without Integer Conversion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two non-negative integers represented as strings, return their sum as a string. You must solve the problem without using any built-in library for handling large integers and without directly converting the inputs to integers. ### Function Signature ```python def add_strings(num1: str, num2: str) -> str: ``` ### Rules - `num1` and `num2` are base-10 strings. Converting a single digit character to its value is allowed. Converting a whole input string with `int()` or a similar call, or using a big-integer library, is not. - Return the sum in base 10 with no leading zeros. A sum of zero is returned as `"0"`. ### Constraints - `1 <= len(num1), len(num2) <= 10^4` - `num1` and `num2` consist of only digits. - `num1` and `num2` have no leading zeros, except for the number zero itself. ### Examples ```text Input: num1 = "11", num2 = "23" Output: "34" ``` No column produces a carry: 1 + 3 = 4 in the ones place and 1 + 2 = 3 in the tens place. ```text Input: num1 = "456", num2 = "77" Output: "533" ``` The ones column gives 6 + 7 = 13 (write 3, carry 1) and the tens column gives 5 + 7 + 1 = 13 (write 3, carry 1). The shorter input has no hundreds digit, so that column is 4 + 0 + 1 = 5. ```text Input: num1 = "1", num2 = "9999" Output: "10000" ``` Every column overflows, and the carry left after the last column becomes a new leading digit, so the result is longer than both inputs.

Overview: Add two non-negative integers that arrive as decimal strings of up to 10,000 digits and return the sum as a string, without converting the inputs to integers or using a big-integer library. It tests digit-by-digit addition with a carry, inputs of different lengths, and building the output in linear time.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two non-negative integers represented as strings, return their sum as a string. You must solve the problem without using any built-in library for handling large integers and without directly converting the inputs to integers.

Function Signature

def add_strings(num1: str, num2: str) -> str:

Rules

  • num1 and num2 are base-10 strings. Converting a single digit character to its value is allowed. Converting a whole input string with int() or a similar call, or using a big-integer library, is not.
  • Return the sum in base 10 with no leading zeros. A sum of zero is returned as "0" .

Constraints

  • 1 <= len(num1), len(num2) <= 10^4
  • num1 and num2 consist of only digits.
  • num1 and num2 have no leading zeros, except for the number zero itself.

Examples

Input: num1 = "11", num2 = "23"
Output: "34"

No column produces a carry: 1 + 3 = 4 in the ones place and 1 + 2 = 3 in the tens place.

Input: num1 = "456", num2 = "77"
Output: "533"

The ones column gives 6 + 7 = 13 (write 3, carry 1) and the tens column gives 5 + 7 + 1 = 13 (write 3, carry 1). The shorter input has no hundreds digit, so that column is 4 + 0 + 1 = 5.

Input: num1 = "1", num2 = "9999"
Output: "10000"

Every column overflows, and the carry left after the last column becomes a new leading digit, so the result is longer than both inputs.