Time: 0:00
Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).
This bug belongs to the logic errors pattern
The wrong operator, an inverted condition, a formula the right way round but backwards.
What went wrong: If low + high exceeds Integer.MAX_VALUE (2,147,483,647), the sum overflows to a negative number, resulting in a negative midpoint and ArrayIndexOutOfBoundsException.
The fix: Use low + (high - low) / 2 or the unsigned bit-shift (low + high) >>> 1.
Remember: Always calculate midpoints with low + (high - low) / 2 to avoid integer overflow.
The pattern behind it: logic errors.
Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.
<iframe src="https://trybughunt.com/embed/4c02e6ba-37f6-4ebe-80a7-6c59ad0b6100" title="Debugging challenge: Midpoint integer overflow in binary search" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/4c02e6ba-37f6-4ebe-80a7-6c59ad0b6100">TryBugHunt</a></p>