Add Two Integers Given as Strings
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) → StringExamples
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 <= 104aandbcontain only the digits0to9and have no leading zeros, except for the number0itself.
More Jane Street Phone Screen questions
- Maximum-Scoring Consecutive Tree LevelsPHONE SCREEN · Seen Nov 2026
- Tree Selection and VisibilityPHONE SCREEN · Seen Nov 2026
- Load/Store Arithmetic InterpreterPHONE SCREEN · Seen Oct 2026
- Instrument Stack Language JumpsPHONE SCREEN · Seen Oct 2026
- Run a Stack Language InterpreterPHONE SCREEN · Seen Oct 2026
- Positive-Integer Expression EvaluationPHONE SCREEN · Seen Oct 2026
- Angle Between Clock HandsPHONE SCREEN · Seen Jul 2026
- Route Length From Two CyclistsPHONE SCREEN · Seen Jul 2026
- Sort TradesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Painted Cube: Count and RollPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Fizz BuzzBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Double-Base PalindromesxAI · PHONE SCREENxAI interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Add Decimal Strings ExactlyMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Format a Centered Pagination BarMixpanel · PHONE SCREENMixpanel interview questions
- Get Max DeletionsJP Morgan Chase · OAJP Morgan Chase interview questions