FastPrepAdd Two Integers Given as Strings

Add Two Integers Given as Strings

Jane Street logoJane Street● EasyFULLTIMEPHONE SCREEN

Updated

Problem statement

You are given two non-negative integers a and b, each written as a string of decimal digits.

Return their sum as a string of decimal digits with no leading zeros. The numbers can be longer than fixed-size integer types hold, so add them digit by digit, the way you would on paper, instead of converting a whole string to a number.

Function

addStrings(a: String, b: String) → String

Examples

Example 1

a = "456"b = "77"return = "533"

Add from the right: 6 + 7 = 13, write 3 and carry 1. Then 5 + 7 + 1 = 13, write 3 and carry 1. Then 4 + 1 = 5. The sum is 533.

Example 2

a = "9999"b = "1"return = "10000"

Every column gives 9 + 1 = 10 or 9 + 0 + 1 = 10, so each writes 0 and carries 1. The last carry becomes a new leading digit, and the sum 10000 is one digit longer than either input.

Example 3

a = "0"b = "0"return = "0"

Zero plus zero is zero, written as the single digit 0.

Constraints

  • 1 <= a.length, b.length <= 104
  • a and b contain only the digits 0 to 9 and have no leading zeros, except for the number 0 itself.

More Jane Street Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Jane Street interview experiences

All Jane Street interview questionsSee Jane Street hiring insights
Saved
class Solution {
    public String addStrings(String a, String b) {
      // Write your code here.
    }
}
Checking account…