Skip to main content
Binary SearchPatternsAlgorithmsInterview Prep

Binary Search Complete Guide — Every Pattern and Template for 2026 Interviews

· 12 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Master every interview binary search pattern: classic search, rotated arrays, boundary finding, and binary search on answer space with code templates.

The binary search mental model

Binary search works on any **monotonic** function — not just sorted arrays. The core principle: if you can evaluate a yes/no condition at any point in a search space, and the answers are of the form "no, no, no, YES, YES, YES", you can binary search for the boundary in O(log n). The classic sorted array search is the simplest case: "is arr[mid] equal to target?" But the same logic applies to: "is this speed fast enough to ship all packages in D days?" "is this value a valid peak?" "can K workers finish in mid days?" Any monotonic condition can be binary searched.

Template 1 — Classic binary search

Find a target in a sorted array. Returns -1 if not found. ```python def binary_search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 # avoids integer overflow if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1 ``` Key: `while left <= right` with `left = mid + 1` and `right = mid - 1`. This terminates because the search space strictly shrinks each iteration.

Template 2 — Find first/last occurrence (boundary finding)

Find the leftmost position where a condition becomes true. Canonical example: first occurrence of a target, or first position ≥ target. ```python def find_left_boundary(nums, target): left, right = 0, len(nums) # note: right = len(nums), not len-1 while left < right: # note: strict less than mid = left + (right - left) // 2 if nums[mid] < target: left = mid + 1 else: right = mid # note: right = mid, not mid - 1 return left # left == right, first position where nums[left] >= target ``` For rightmost occurrence: use `nums[mid] <= target → left = mid + 1`, else `right = mid`, return `left - 1`. Problems: Find First and Last Position (LC 34), Search Insert Position (LC 35).

Template 3 — Rotated sorted array

A sorted array is rotated at an unknown pivot. Strategy: one half is always sorted — identify which half and check if the target is in that range. ```python def search_rotated(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid # Left half is sorted if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 # Right half is sorted else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 ``` Problems: Search in Rotated Sorted Array (LC 33), Find Minimum in Rotated Sorted Array (LC 153).

Template 4 — Binary search on answer (most powerful)

The search space is not an array — it is a range of possible answers. You define a condition check function and binary search for the minimum (or maximum) valid answer. **Pattern:** ```python def solve(params): def condition(value) -> bool: # Returns True if `value` is a valid answer # Must be monotonic: all False ... then all True pass left, right = min_possible_answer, max_possible_answer while left < right: mid = left + (right - left) // 2 if condition(mid): right = mid # try smaller else: left = mid + 1 # too small, try larger return left ``` **Koko Eating Bananas (LC 875) — worked example:** ```python def minEatingSpeed(piles, h): def canFinish(speed): return sum((pile - 1) // speed + 1 for pile in piles) <= h left, right = 1, max(piles) while left < right: mid = left + (right - left) // 2 if canFinish(mid): right = mid else: left = mid + 1 return left ``` **Problems using this template:** Koko Eating Bananas (LC 875), Minimum Number of Days to Make m Bouquets (LC 1482), Capacity to Ship Packages (LC 1011), Split Array Largest Sum (LC 410), Minimize Maximum Distance to Gas Stations (LC 774).

Recognizing binary search problems

The signal that a problem wants binary search is often hidden. Ask yourself: 1. **Is the data sorted?** → Classic binary search or rotated array variant. 2. **Does the problem ask for a minimum/maximum value satisfying a constraint?** → Binary search on answer. 3. **Is there a monotonic yes/no function over a continuous range?** → Binary search on answer. 4. **Does a brute force of O(n²) or O(n log n) come to mind, but the constraint is n ≤ 10⁹?** → The answer space suggests binary search + a linear/log check function. Keywords: "minimum speed such that", "maximum number of", "feasible in D days", "smallest valid value", "split array into k parts to minimize maximum". Time complexity: binary search itself is O(log n). Total complexity = O(log(answer_range) × cost_of_condition_check). For Koko: O(log(max_pile) × n) where n is the number of piles.

Frequently Asked Questions

What is binary search and when should I use it?
Binary search is a divide-and-conquer algorithm that finds a target in a sorted collection in O(log n) time by repeatedly halving the search space. Use it whenever: (1) the input is sorted, (2) the problem asks for a specific target or the smallest/largest value satisfying a condition, (3) the answer space is monotonic (everything before point X fails, everything after passes), or (4) a problem asks you to find a boundary in a yes/no decision function.
What is the most common binary search bug in interviews?
Off-by-one errors in the boundary conditions. The two most common bugs: (1) infinite loop when mid = left and you assign left = mid (use left = mid + 1 for searches), and (2) wrong answer because you used strict < vs <= in the while condition. The safest template: while (left < right) with mid = left + (right - left) // 2, then decide whether to move left = mid + 1 or right = mid based on whether the condition holds at mid.
What are the different types of binary search patterns?
There are four core binary search patterns: (1) Classic — find target in sorted array, (2) Find boundary — find first/last occurrence of a condition, (3) Rotated array — sorted array rotated at an unknown pivot, and (4) Binary search on answer — the search space is not an array but a range of possible answers, and you binary search for the minimum/maximum valid answer. Pattern 4 is the most powerful and least obvious.
What are the most important binary search problems for FAANG interviews?
Must-know problems: Binary Search (LC 704, trivial), Find First and Last Position (LC 34, boundary finding), Search in Rotated Sorted Array (LC 33, rotation handling), Koko Eating Bananas (LC 875, binary search on answer), Minimum in Rotated Sorted Array (LC 153), Median of Two Sorted Arrays (LC 4, Hard), Find Peak Element (LC 162), Split Array Largest Sum (LC 410, binary search on answer Hard), and Capacity to Ship Packages (LC 1011).

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer II · ServiceNow · NIT Puducherry

Software Engineer II at ServiceNow, previously PayPal India and NCR Atleos. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee
Daily Practice
0/0