Skip to content
CodeKruPro

Add two very large numbers given as strings

Coding problemMediumAsked in: PhonePe

Given two non-negative integers a and b written as strings of decimal digits, return their sum as a string of digits.

The numbers can have up to 100,000 digits, far more than any built-in integer type holds, so add them digit by digit. Neither input has leading zeros, except the number 0 itself, and the result must not have leading zeros either.

Examples

Example 1
Input: a = "11", b = "123"
Output: "134"
Example 2
Input: a = "999", b = "1"
Output: "1000"
Example 3
Input: a = "0", b = "0"
Output: "0"

Constraints

  • a and b each have between 1 and 100,000 digits
  • They hold only the digits 0 to 9, with no leading zeros unless the number is 0

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in