CHAPTER 09
Beginner
Binary Search Techniques
Updated: May 18, 2026
5 min read
# CHAPTER 9
Binary Search Techniques
1. Chapter Introduction
If an interviewer hands you a problem involving a Sorted Array and asks for an optimal solution, your brain must instantly scream: "Binary Search!" Linear search checks every element, taking O(N) Time. Binary Search eliminates half of the remaining elements with every single step, resulting in blazing fast O(log N) Time. This chapter covers the standard template, preventing integer overflow, and advanced search space reduction (like rotated arrays).2. The Core Concept
Imagine playing a guessing game. "I am thinking of a number between 1 and 100." If you guess 1, 2, 3... (Linear Search), it could take 100 guesses. If you guess 50, and I say "Higher", you instantly eliminate 1-50. You next guess 75. By always guessing the middle, you can find the number in a maximum of 7 guesses. That is Logarithmic Time (Base 2).log2(100) ≈ 6.64.
3. The Standard Binary Search Template
This is the one algorithm you must memorize line-by-line. Do not try to improvise this in an interview.*Prompt:* Find the index of target in a sorted array nums.
python
4. Preventing Integer Overflow (The mid calculation)
Why do we calculate mid as left + (right - left) // 2 instead of the obvious (left + right) // 2?
In languages like Java or C++, integers have a maximum limit (~2.14 billion). If left and right are both very large indices (e.g., 1.5 billion), doing left + right will equal 3 billion, which exceeds the integer limit and crashes the program.
The formula left + (right - left) / 2 calculates the exact same middle point without ever causing the sum to exceed the maximum value.
5. Pattern 1: Search Space Reduction (The Answer is NOT the exact target)
Sometimes you aren't looking for an exact match. *Example: First Bad Version (LeetCode #278)* *Prompt:* You have versions[Good, Good, Bad, Bad, Bad]. Find the *first* bad version.
*Logic:* If you check the middle and it is Bad, you can't just return it. It might be the 3rd bad version. You must record it as a potential answer, but continue searching the left half to see if there is an *earlier* bad version.
java
6. Pattern 2: Search in a Rotated Sorted Array
This is a heavily tested "Hard" concept often asked at FAANG. *Prompt:* A sorted array was rotated at a pivot.[0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]. Search for a target in O(log N) time.
*The Trick:* Even though the whole array isn't sorted, one half of the array is ALWAYS perfectly sorted.
-
1.
Find
mid.
-
2.
Check if the Left side is perfectly sorted (is
nums[left] <= nums[mid]).
- If Left is sorted: Is the target within this left range? If yes, discard Right. If no, discard Left.
- 3. If Left is not sorted, the Right side MUST be perfectly sorted.
- Is the target within the right range? If yes, discard Left. If no, discard Right.
7. Real-World Scenario: Binary Search on the Answer Space
Binary search isn't just for arrays. You can Binary Search on a range of numbers! *Prompt (Koko Eating Bananas):* Koko can eatK bananas per hour. Find the minimum K required to eat all bananas within H hours.
*Logic:*
- Min speed = 1 banana/hr.
- Max speed = Max pile size/hr.
-
Binary search between 1 and Max. Check
mid. Can she finish in time at speedmid? If yes, try a slower speed (discard right half). If no, try a faster speed (discard left half).
8. Mini Project: Find Minimum in Rotated Sorted Array
*Prompt:* Given[3, 4, 5, 1, 2], find the minimum element (1) in O(log N) time.
*Logic:*
-
If
nums[mid] > nums[right], we are in the high slope. The minimum MUST be to the right.left = mid + 1.
-
If
nums[mid] <= nums[right], we are in the low slope. The minimum is here or to the left.right = mid.
9. Common Mistakes
-
Infinite Loops (Off-by-One): The condition
while left <= rightis standard. However, if you update boundaries usingleft = midandright = mid(instead ofmid + 1ormid - 1), the pointers can get stuck continuously checking the same middle element forever.
- Forgetting the Sorted Requirement: You cannot run Binary Search on an unsorted array. If you need to search, you must sort it first, which costs O(N log N) time, making the overall algorithm O(N log N).
10. Best Practices
-
Draw the Number Line: In an interview, draw a line. Plot
L,M, andR. EraseLand move it pastM. Visualizing the boundary shifts prevents index errors.
11. Exercises
-
1.
Trace the standard Binary Search for
target = 7in[1, 3, 5, 7, 9]. What are the values of left, right, and mid at each step?
- 2. Why is Binary Search exponentially faster than Linear Search?
12. MCQs
Question 1
What is the fundamental prerequisite required to perform a Binary Search on an array?
Question 2
What is the Time Complexity of Binary Search?
Question 3
What is the standard and safest while loop condition for basic Binary Search?
Question 4
Why should you calculate the middle index using mid = left + (right - left) / 2 instead of (left + right) / 2?
Question 5
When the target is strictly LARGER than nums[mid], how do you update the boundaries?
Question 6
What is a common cause of an "Infinite Loop" in a poorly written Binary Search?
Question 7
In the "First Bad Version" problem, what do you do when nums[mid] is a Bad Version?
Question 8
When searching a "Rotated Sorted Array" (e.g., [4,5,6,7,0,1,2]), what key property makes O(log N) search possible?
Question 9
Can Binary Search be applied to things other than Arrays?
Question 10
If an unsorted array is passed into your Binary Search function, what will happen?
14. Interview Questions
-
Q: "Given an array of integers
numssorted in non-decreasing order, find the starting and ending position of a giventargetvalue. If target is not found in the array, return [-1, -1]." (LeetCode 34. Hint: Run Binary Search twice).
15. FAQs
- Q: Does Python have a built-in Binary Search?
bisect module (bisectleft and bisectright). However, in interviews, you are usually expected to write it from scratch to prove you understand the index boundary updates.
16. Summary
Binary Search is the ultimate tool for achieving O(log N) Time on sorted data. Memorize the template:while left <= right, calculate mid safely to avoid overflow, and strictly update left = mid + 1 or right = mid - 1. Master the advanced patterns: finding the "first occurrence" of an element, and identifying the sorted half in a rotated array.