Binary Search Algorithm Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you can state the sorted-array precondition, write the loop without off-by-one errors, and justify the O(log n) bound
Computing mid as (left + right) / 2 (overflows for large indices) and updating boundaries to mid instead of mid +/- 1, causing infinite loops
Say each comparison halves the range, so ~20 steps cover a million elements, and use the overflow-safe mid formula without being asked
Interview-ready guide
Interview answer
Binary search is a divide-and-conquer way to find a value in a sorted array, and it runs in O of log n time.
I keep two pointers, left at the start and right at the end of the array. In each step I look at the middle element. If it equals my target, I'm done and I return that index.
If the target is smaller than the middle, then because the array is sorted I know it can only be in the left half, so I move right to mid minus one. If the target is larger, it must be in the right half, so I move left to mid plus one. I repeat until I either find the value or the pointers cross, which means it isn't there.
The reason this is so fast is that every single comparison throws away half of what's left, so even a million elements only take about twenty comparisons, compared to up to a million for a plain linear scan. Two things I'm always careful about as a fresher.
First, I compute the middle as left plus the quantity right minus left divided by two, instead of left plus right divided by two, because adding two large indices can overflow an int and wrap to a negative number. Second, the array genuinely has to be sorted first, otherwise the middle element tells me nothing about which side to keep, and the whole elimination idea breaks down.
The iterative version uses constant extra space; the recursive version uses O of log n space for the call stack.
Binary Search Algorithm Java | Fresher Interview
Direct Answer
Binary search finds a target in a sorted array by repeatedly halving the search range. Compare the target with the middle element: if equal you're done, if the target is smaller search the left half, if larger search the right half. Each step throws away half the data, giving O(log n) time and O(1) space for the iterative version.
Code Example
int[] nums = {2, 5, 8, 12, 16, 23, 38, 45, 56}; // indices 0..8
int target = 23;
// Step 1: left=0, right=8 -> mid=4, nums[4]=16 < 23 -> left=5
// Step 2: left=5, right=8 -> mid=6, nums[6]=38 > 23 -> right=5
// Step 3: left=5, right=5 -> mid=5, nums[5]=23 == 23 -> return 5
System.out.println(binarySearch(nums, 23)); // prints 5
Three comparisons locate the target in a 9-element array; the range shrank 9 -> 4 -> 1.
Common Mistakes & Pitfalls
- Overflowing mid:
mid = (left + right) / 2overflows whenleft + rightexceedsInteger.MAX_VALUE. Usemid = left + (right - left) / 2. - Wrong boundary move: setting
left = midorright = mid(without +/- 1) can loop forever when only two elements remain. Always move pastmid. - Unsorted input: binary search on unsorted data returns garbage. Sort first, or use a different structure.
- Wrong loop condition:
while (left < right)can miss the last element; the inclusive template usesleft <= right.
Check your understanding
Answer these without looking back at the article.
- Why does mid = (left + right) / 2 cause overflow?
- How do you find the first occurrence of a duplicate element?
- How do you apply binary search on a rotated sorted array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.