Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `a + b`. If the inputs can be near the 32-bit limit, use a 64-bit type (`long` in Java) for the result so the addition doesn't overflow.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reflexively consider the value range of an arithmetic result, not just the logic.
Where candidates slip
Adding two large ints into an int and returning a silently wrapped negative number.
How to stand out
Volunteer the overflow case unprompted, explain `(long) a + b` vs `(long)(a + b)`, and note Python's unbounded ints make it a non-issue there.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Add directly
1Hint 1Explore hintHide hint
The operation is just `a + b`. So what is the interviewer really checking?
2Hint 2Explore hintHide hint
Think about the largest values `a` and `b` can take. Does the sum still fit in a 32-bit `int`?
3Hint 3Explore hintHide hint
Promote one operand to a 64-bit type before adding so the addition itself can't overflow.
A · Core idea
There is no algorithm to discover here — the answer is the + operator. What an interviewer is actually probing on this 'warm-up' is whether you think about the range of the result, because that is the habit that prevents real bugs later.
Why overflow is the whole point. A 32-bit signed int holds values up to about 2.1 billion (2³¹ − 1). If a and b are each near 10⁹, their sum is near 2×10⁹, which is still inside int range — but two values near the int ceiling sum to ~4.3 billion, which silently wraps around to a negative number. The fix is to perform the addition in 64-bit by widening one operand to long before the + happens.
Why cast one operand, not the result. Writing (long)(a + b) is too late: a + b is already computed in int and may have overflowed before the cast runs. (long) a + b promotes b to long as well (Java's binary numeric promotion), so the whole addition is 64-bit.
Python note. Python integers are arbitrary precision, so there is no overflow — a + b is always exact. The overflow discussion is a Java/C++/Go concern, and saying so out loud shows you understand why the languages differ.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = 2000000000, b = 2000000000int + int
2000000000 + 2000000000 = 4000000000, but that exceeds 2³¹−1
wraps to -294967296 (BUG)4000000000C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Freshers are tested less on the math and more on awareness of data-type limits. Mentioning overflow on a 'trivial' question is exactly the kind of detail that stands out.
ComplexityO(1) time, O(1) space — a single addition is one machine instruction, independent of the input values, so the work is constant.
E · Edge cases and pitfalls5a = 0, b = 0 — Returns 0 — the additive identity, no special handling needed.Explore edge casesHide edge cases
Edge cases handled
a = 0, b = 0Returns 0 — the additive identity, no special handling needed.a = -2000000000, b = -2000000000Sum -4000000000 underflows int but is exact in long.a = 10^9, b = -10^9Returns 0; mixed signs cancel with no range concern.
Pitfalls
- Casting after the addition — `(long)(a + b)` — which overflows before the cast.
- Assuming the sum 'obviously' fits because each input fits; two near-max ints overflow when added.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long sum(int a, int b) { return (long) a + b; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Casting too late
`(long)(a + b)` computes `a + b` in 32-bit first, so the overflow has already happened before the widening cast.
Explore the code comparisonHide the code comparison
Casting too late
`(long)(a + b)` computes `a + b` in 32-bit first, so the overflow has already happened before the widening cast.
Don't write this
long sum = (long)(a + b); // a + b overflowed in int alreadyWrite this instead
long sum = (long) a + b; // promotes b too, adds in 64-bitRevise
Carry the pattern into the interview
Pattern to remember
Direct arithmetic + overflow awareness
Code pattern to memorize
return (long) a + b;Rules to remember
- 1Return
a + b. - 2Widen to
longwhen operands can be large. - 3Python ints are unbounded — no overflow there.
Use this pattern when
- Combining two integers whose sum may approach the type limit
- Any place a 'trivial' add hides an overflow
Choose another pattern when
- Inputs guaranteed tiny — a plain int add is fine
- Working in Python, where ints are unbounded
Interview takeaway
Even the simplest question rewards mentioning integer overflow.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Direct arithmetic + overflow awareness. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Add Two Numbers (linked lists)Coming soonSame addition but digit-by-digit with carry over two linked lists.
- Sum of Array ElementsGeneralises one addition into a fold over n values.