Binary Search
Binary Search Fundamentals
Master the core binary search pattern, integer overflow avoidance, search invariants, and standard exact-match implementations.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What is Binary Search?
Binary Search is an efficient searching algorithm that operates on sorted collections. It repeatedly halves the size of the search space, checking the middle element and narrowing the target range until the element is found or the range becomes empty.Why is it Important?
Linear search takesO(N) comparisons. Binary Search takes only O(log N) comparisons.
At scale, this difference is staggering:
N = 1,000,000 (1 million), linear search takes up to 1,000,000 steps. Binary search takes at most 20 steps.N = 1,000,000,000 (1 billion), binary search takes at most 30 steps.Where is it Used?
2. Mental Model: The Guessing Game
Imagine someone asks you to guess a number between 1 and 100.
50, they tell you: "My number is higher."[1..50]. Your new search space is [51..100].75. They tell you: "My number is lower."[75..100]. Your new search space is [51..74].Each guess costs O(1) time, but eliminates 50\% of the possibilities. This geometric decay is the essence of logarithmic time complexity.
3. Core Concepts & Implementations
1. Invariant & Termination
In exact-match binary search, we maintain the loop invariant that the target is within the range[lo, hi].
lo = 0 and hi = n - 1.while (lo <= hi) because if lo == hi, there is still one element left to check.lo > hi, the search space is empty, indicating the target is not present.2. Avoiding Integer Overflow
A common rookie mistake is calculating mid asint mid = (lo + hi) / 2. If the sum of lo and hi exceeds the maximum integer capacity (2^31 - 1 in Java/C++), it overflows to a negative number, causing index-out-of-bounds errors.
Always use the safe formula:
mid = lo + \frac{hi - lo}{2}
4. Visual Trace: Finding Target = 13
Let's search for 13 in the sorted array [2, 5, 8, 12, 13, 17, 20]:
5. Real-World Applications
B branches instead of 2, speeding up disk block reads.6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test basic setup invariants:Common Mistakes
Warning: 1. Incorrect Index Boundaries: Initializing
hi = array.length instead of array.length - 1 for exact-match searches. This will trigger index out of bounds exceptions when checking arr[mid].> 2. Infinite Loops: Forgetting to update boundary parameters (lo = mid + 1orhi = mid - 1) and instead settinglo = midorhi = midcan lead to infinite loops whenloandhidiffer by 1.
7. Summary
O(log N); space complexity is O(1).lo + (hi - lo) / 2.lo <= hi for exact matches, and adjust boundaries excluding mid.8. Quiz
Question 1: What is the maximum number of comparisons needed to search for an element in a sorted array of size 1024?
Answer: 11 comparisons.log_2(1024) = 10. If the target is not found, we perform one final check when the loop condition terminates, making it at most 11 checks.
Question 2: What is the consequence of calculating mid as (lo + hi) / 2 in Java or C++?
Answer: Iflo + hi exceeds 2^31 - 1, it overflows to a negative integer. Dividing a negative number by 2 yields a negative index, causing an ArrayIndexOutOfBoundsException.
Question 3: If an array contains duplicates, does the standard exact-match template guarantee finding the first occurrence?
Answer: No. The exact-match template returns the index of any matching element it encounters first, which depends on the position of the elements. To find the first occurrence, a boundary template must be used.Question 4: What is the recurrence relation for Binary Search?
Answer:T(N) = T(N/2) + O(1). According to Case 2 of the Master Theorem, this evaluates to O(log N).
Question 5: Can you apply binary search on a sorted Singly Linked List?
Answer: You can, but it is highly inefficient. Because linked lists lack random access, findingmid takes O(N) traversal steps. This degrades the overall search time to O(N), defeating the purpose of binary search.