oneby7

Foundations Data types

584. Add Two Large Numbers

Easy

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

Output: 12000000000

Explanation: 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

Output: -4500000000

Explanation: −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

Output: 2147483648

Explanation: 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.

a = -2147483648, b = -2147483648

Constraints

  • -9 × 1015 ≤ a ≤ 9 × 1015
  • -9 × 1015 ≤ b ≤ 9 × 1015

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.