Foundations Data types
584. Add Two Large Numbers
Given two integers a and b, return their sum, a + b.
The numbers can be negative, and they can be much larger than a 32-bit integer can hold. Return the exact sum.
The sum can be larger than 253, so the JavaScript starter returns a BigInt.
Example 1
Input: a = 5000000000, b = 7000000000
12000000000Explanation: 5,000,000,000 + 7,000,000,000 = 12,000,000,000. Both numbers are past 2,147,483,647, the largest value a 32-bit integer can hold, and so is their sum.
Example 2
Input: a = -7000000000, b = 2500000000
-4500000000Explanation: −7,000,000,000 + 2,500,000,000 = −4,500,000,000. A negative sum can be too large for 32 bits as well.
Example 3
Input: a = 2147483647, b = 1
2147483648Explanation: 2,147,483,647 is the largest 32-bit integer, and the sum is one more than that.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
-9 × 1015 ≤ a ≤ 9 × 1015-9 × 1015 ≤ b ≤ 9 × 1015
Clarifying questions
What you would ask an interviewer before writing any code.