Binary Search Mid Calculation Overflow
Fast recall
Quick revision
Core answer
left + right can overflow a 32-bit int when both are near Integer.MAX_VALUE, wrapping to a negative number and producing a garbage mid. left + (right - left) / 2 is mathematically the same midpoint but keeps the intermediate value bounded, because right - left is always smaller than either endpoint. Java overflow is silent - no exception - so the bug shows up as wrong results.Awareness of silent integer overflow and attention to detail in a 'trivial' line of code
Assuming Java throws on overflow, or thinking (left + right) / 2 is always fine because it works on small arrays
State the exact failure condition - left + right exceeding Integer.MAX_VALUE - and that it matters for answer-space searches up to 2 billion
Interview-ready guide
Interview answer
I use left plus the quantity right minus left divided by two instead of left plus right divided by two because the simpler version can overflow.
In Java an int is a 32-bit signed value, so it maxes out around two point one billion. If left and right are both large, and that really happens when you binary search an answer space that can go up to about two billion rather than just a small array, then adding them together produces a sum bigger than the maximum an int can hold.
The dangerous part is that Java doesn't throw an exception when this happens. It silently wraps around to a negative number, so your middle index becomes negative and the search either crashes with an out-of-bounds access or quietly returns the wrong answer. The safe formula avoids ever forming that oversized sum.
Right minus left is just the size of the current range, which is always smaller than either endpoint, so it can't overflow. I halve that gap and add it back to left, and the result is exactly the same midpoint, just computed in a way that stays within bounds. What I'd emphasize in an interview is that this costs nothing.
It's the same number of operations, so there's no reason not to write it the safe way every time. It signals that I think about edge cases and that I know integer overflow in Java is silent rather than something the runtime will catch for me. It's a one-line habit that prevents a rare but genuinely hard-to-debug failure in production code that handles large ranges.
Binary Search Mid Calculation Overflow
Direct Answer
left + right can overflow a 32-bit int when both are near Integer.MAX_VALUE, wrapping to a negative number and producing a garbage mid. left + (right - left) / 2 is mathematically the same midpoint but keeps the intermediate value bounded, because right - left is always smaller than either endpoint. Java overflow is silent - no exception - so the bug shows up as wrong results.
Code Example
// UNSAFE: left + right can exceed Integer.MAX_VALUE and wrap negative
int midUnsafe = (left + right) / 2;
// SAFE: right - left is bounded by the range size, never overflows
int midSafe = left + (right - left) / 2;
// Demonstration of the wrap:
int left = 2_000_000_000, right = 2_000_000_000;
System.out.println((left + right) / 2); // -147483648 (overflowed!)
System.out.println(left + (right - left) / 2); // 2000000000 (correct)
Both lines do the same arithmetic when values are small; only the safe form survives large indices.
Common Mistakes & Pitfalls
- Thinking Java warns you: integer overflow is silent - no
ArithmeticException, just wrong values. - 'It always works':
(left + right) / 2works until indices get large (answer-space binary search), then fails rarely and mysteriously. - Vague explanation: saying 'it overflows' without naming the cause - two large positive ints summing past
Integer.MAX_VALUEand wrapping negative.
Check your understanding
Answer these without looking back at the article.
- What is Integer.MAX_VALUE in Java?
- How does unsigned right shift differ from signed right shift?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.