Binary Search Interview Problems: Patterns, Pitfalls & Winning Strategies
Binary search shows up in roughly 15-20% of technical screening rounds at top tech companies — not because it's exotic, but because it's a clean signal for how a candidate thinks about elimination, boundaries, and edge cases under pressure. Miss the pattern and you'll write an O(n) linear scan where an O(log n) search was expected. Spot it and you look like the engineer who actually understands algorithmic trade-offs.
The real problem isn't the algorithm itself — most people can recite 'split in half, compare, repeat'. The problem is recognizing WHEN to apply it, and then getting the boundary conditions exactly right without an off-by-one bug that only surfaces on arrays of length 1 or 2. Interviewers know this, which is why they love edge cases like single-element arrays, all-duplicate arrays, and rotated sorted arrays.
By the end of this article you'll be able to identify the four core binary search patterns by sight, implement each one without looking up the boundary conditions, and explain your reasoning out loud — which is exactly what interviewers want to hear. We'll go from the textbook template all the way through rotation, condition-based search, and answer-space binary search with full runnable code for each.
The Template That Eliminates Off-By-One Bugs Forever
Every binary search variant is a specialization of one template. Until you've internalized this template so deeply that you can write it in your sleep, you'll keep making subtle off-by-one errors that cause infinite loops or miss the target by one index.
The key design decision in any binary search is: what does your loop invariant guarantee? The invariant we'll use is this — the answer, if it exists, is always inside the closed interval [left, right]. That single rule dictates every boundary choice that follows.
When left == right there's exactly one candidate left. That's why the loop condition is left <= right, not left < right. And when you calculate mid, you use left + (right - left) / 2 instead of (left + right) / 2 — both give the same index, but the second one silently overflows when left and right are large integers, a bug that killed a famous Java binary search implementation in the JDK for nearly a decade.
The moment you find target at mid, you return immediately. If target < array[mid], the right half is useless, so right = mid - 1. If target > array[mid], the left half is useless, so left = mid + 1. This is the foundation every other pattern builds on.
public class ClassicBinarySearch { /** * Finds the exact index of a target in a sorted array. * Returns -1 if the target is not present. * * Time: O(log n) — we halve the search space on every iteration * Space: O(1) — no extra memory beyond a few pointers */ public static int findExactIndex(int[] sortedPrices, int targetPrice) { int left = 0; int right = sortedPrices.length - 1; // Loop invariant: if targetPrice exists, it lives in sortedPrices[left..right] while (left <= right) { // Safe mid-point: avoids integer overflow for large indices int mid = left + (right - left) / 2; if (sortedPrices[mid] == targetPrice) { return mid; // Found it — return immediately } else if (sortedPrices[mid] < targetPrice) { left = mid + 1; // Target is in the RIGHT half } else { right = mid - 1; // Target is in the LEFT half } } return -1; // left has crossed right — target is not in the array } public static void main(String[] args) { int[] sortedPrices = {12, 25, 37, 49, 56, 68, 74, 89, 95}; System.out.println(findExactIndex(sortedPrices, 56)); // Found at index 4 System.out.println(findExactIndex(sortedPrices, 89)); // Found at index 7 System.out.println(findExactIndex(sortedPrices, 100)); // Not present → -1 System.out.println(findExactIndex(sortedPrices, 12)); // Edge: first element → 0 System.out.println(findExactIndex(sortedPrices, 95)); // Edge: last element → 8 } }
7
-1
0
8
Finding Boundaries: First and Last Position of a Duplicated Target
Once you've nailed the classic template, the first upgrade interviewers throw at you is: 'What if the array has duplicates? Find the first occurrence, or the last occurrence.' This is LeetCode 34, and it's a direct test of whether you understand binary search deeply or just memorized the basic version.
The trick is to NOT return the moment you find the target. Instead, you record the current mid as a candidate answer, then deliberately push the search boundary to keep looking. For the first occurrence you push right = mid - 1 (keep searching left). For the last occurrence you push left = mid + 1 (keep searching right).
This turns a single-target search into a boundary search. The loop still terminates — left and right still converge — but now instead of exiting early you let the loop exhaust all possibilities, always keeping the best answer you've seen so far.
This pattern has a name: Left Boundary Binary Search and Right Boundary Binary Search. Recognizing them as named patterns rather than one-off tricks is what lets you adapt quickly when interviewers change the constraint mid-question.
public class BoundaryBinarySearch { /** * Finds the index of the FIRST occurrence of targetScore in a sorted array. * Returns -1 if not present. * * Key insight: when we find the target, we record it but keep searching LEFT * to see if an earlier occurrence exists. */ public static int findFirstOccurrence(int[] sortedScores, int targetScore) { int left = 0; int right = sortedScores.length - 1; int firstIndex = -1; // Holds the best (leftmost) answer found so far while (left <= right) { int mid = left + (right - left) / 2; if (sortedScores[mid] == targetScore) { firstIndex = mid; // Record this as our current best answer right = mid - 1; // But keep looking LEFT for an earlier one } else if (sortedScores[mid] < targetScore) { left = mid + 1; } else { right = mid - 1; } } return firstIndex; } /** * Finds the index of the LAST occurrence of targetScore in a sorted array. * Returns -1 if not present. * * Key insight: when we find the target, we record it but keep searching RIGHT * to see if a later occurrence exists. */ public static int findLastOccurrence(int[] sortedScores, int targetScore) { int left = 0; int right = sortedScores.length - 1; int lastIndex = -1; // Holds the best (rightmost) answer found so far while (left <= right) { int mid = left + (right - left) / 2; if (sortedScores[mid] == targetScore) { lastIndex = mid; // Record this as our current best answer left = mid + 1; // But keep looking RIGHT for a later one } else if (sortedScores[mid] < targetScore) { left = mid + 1; } else { right = mid - 1; } } return lastIndex; } public static void main(String[] args) { // Sorted array with duplicates — classic interview setup int[] sortedScores = {5, 10, 10, 10, 20, 30, 30, 45}; System.out.println("First 10: " + findFirstOccurrence(sortedScores, 10)); System.out.println("Last 10: " + findLastOccurrence(sortedScores, 10)); System.out.println("First 30: " + findFirstOccurrence(sortedScores, 30)); System.out.println("Last 30: " + findLastOccurrence(sortedScores, 30)); // Count of 10s = lastIndex - firstIndex + 1 int first = findFirstOccurrence(sortedScores, 10); int last = findLastOccurrence(sortedScores, 10); System.out.println("Count of 10: " + (last - first + 1)); // Target not present System.out.println("First 99: " + findFirstOccurrence(sortedScores, 99)); } }
Last 10: 3
First 30: 5
Last 30: 6
Count of 10: 3
First 99: -1
Searching a Rotated Sorted Array — The Classic Hard-Mode Variant
Here's where interviewers separate people who 'know binary search' from people who truly understand it. A rotated sorted array is an array like [40, 55, 63, 7, 18, 29] — it was originally sorted but has been rotated around a pivot. Half the array is still sorted. That's the key insight.
At every mid-point you can determine in O(1) which half is sorted — just compare array[left] with array[mid]. If array[left] <= array[mid], the left half is cleanly sorted. Otherwise, the right half is. Once you know which half is sorted, you can check in O(1) whether the target falls inside that sorted half. If yes, search there. If no, search the other half.
You never need to find the pivot explicitly. The binary search logic handles everything in one pass. That's the elegant part — and it's also what trips people up who try to pre-compute the pivot index before searching. Two passes where one will do is a sign of incomplete understanding, and interviewers notice.
public class RotatedArraySearch { /** * Searches for a target in a sorted-but-rotated array. * * Example rotation: [10, 20, 30, 40, 50] rotated at index 3 * => [40, 50, 10, 20, 30] * * Strategy: at every mid, figure out which half IS sorted, * then decide which half the target belongs to. * * Time: O(log n) * Space: O(1) */ public static int searchRotatedArray(int[] rotatedTemps, int targetTemp) { int left = 0; int right = rotatedTemps.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (rotatedTemps[mid] == targetTemp) { return mid; // Lucky — found it at the mid-point } // Determine which half is the "clean" sorted half if (rotatedTemps[left] <= rotatedTemps[mid]) { // LEFT half is cleanly sorted (no rotation in it) if (targetTemp >= rotatedTemps[left] && targetTemp < rotatedTemps[mid]) { // Target is inside the clean left half — search there right = mid - 1; } else { // Target must be in the messy right half left = mid + 1; } } else { // RIGHT half is cleanly sorted (no rotation in it) if (targetTemp > rotatedTemps[mid] && targetTemp <= rotatedTemps[right]) { // Target is inside the clean right half — search there left = mid + 1; } else { // Target must be in the messy left half right = mid - 1; } } } return -1; // Target not found in the array } public static void main(String[] args) { // Original sorted: [7, 18, 29, 40, 55, 63] // Rotated at pivot index 3: [40, 55, 63, 7, 18, 29] int[] rotatedTemps = {40, 55, 63, 7, 18, 29}; System.out.println(searchRotatedArray(rotatedTemps, 18)); // In right segment → 4 System.out.println(searchRotatedArray(rotatedTemps, 55)); // In left segment → 1 System.out.println(searchRotatedArray(rotatedTemps, 40)); // First element → 0 System.out.println(searchRotatedArray(rotatedTemps, 29)); // Last element → 5 System.out.println(searchRotatedArray(rotatedTemps, 100)); // Not present → -1 // Edge: single element int[] single = {42}; System.out.println(searchRotatedArray(single, 42)); // → 0 System.out.println(searchRotatedArray(single, 99)); // → -1 } }
1
0
5
-1
0
-1
Answer-Space Binary Search — When the Input Isn't Even an Array
This is the pattern that separates intermediate from advanced binary search practitioners — and it trips up even experienced engineers. Answer-space binary search applies when you can't binary search the input directly, but you CAN binary search the space of possible answers.
The classic examples: 'Find the minimum speed to ship packages within D days' (LeetCode 1011), 'Koko eating bananas' (LeetCode 875), 'Split array largest sum' (LeetCode 410). None of these have a sorted array to search through. Instead, they have a range of valid answers, and a monotonic condition — if answer X is feasible, then any answer larger than X is also feasible. That monotonicity is your sorted array.
The template is: define a minimum and maximum possible answer (your left and right pointers). Write a feasibility function that checks 'is this candidate answer good enough?'. Then binary search over the answer space, always recording the best valid answer found so far. This is the most powerful and least-recognized form of binary search in interview settings.
public class AnswerSpaceBinarySearch { /** * PROBLEM: You're shipping packages with given weights over D days. * The ship has a weight capacity limit per day — packages load in order. * Find the MINIMUM possible capacity that lets you ship all packages in D days. * * WHY binary search? If capacity C works, then C+1 also works (more capacity = easier). * That monotonic relationship means we can binary search the answer space. * * Answer space: * Minimum possible = max(packageWeights) — must fit heaviest single package * Maximum possible = sum(packageWeights) — ship everything in one day */ public static int minimumShipCapacity(int[] packageWeights, int daysAllowed) { // Left = smallest valid capacity (can't be less than the heaviest package) int left = 0; int right = 0; for (int weight : packageWeights) { left = Math.max(left, weight); // Must fit the heaviest single package right += weight; // Could always ship everything in 1 day } int minimumCapacity = right; // Worst-case starting answer while (left <= right) { int candidateCapacity = left + (right - left) / 2; if (canShipWithinDays(packageWeights, daysAllowed, candidateCapacity)) { // This capacity works — record it, then try to go LOWER (more efficient) minimumCapacity = candidateCapacity; right = candidateCapacity - 1; } else { // This capacity is too small — we need more left = candidateCapacity + 1; } } return minimumCapacity; } /** * Feasibility check: can we ship all packages within the day limit * using exactly this capacity per day? * * We greedily load each day's ship until adding the next package * would exceed the capacity, then start a new day. */ private static boolean canShipWithinDays( int[] packageWeights, int daysAllowed, int capacity) { int daysNeeded = 1; // We always need at least 1 day int currentLoad = 0; // Weight loaded onto today's ship so far for (int weight : packageWeights) { if (currentLoad + weight > capacity) { // Adding this package would exceed today's limit — start a new day daysNeeded++; currentLoad = 0; if (daysNeeded > daysAllowed) { return false; // Already exceeded our day budget — not feasible } } currentLoad += weight; } return true; // Shipped everything within the allowed days } public static void main(String[] args) { // Packages must ship in ORDER (can't reorder them) int[] packageWeights = {3, 2, 8, 4, 7, 5, 1, 6}; int daysAllowed = 4; int result = minimumShipCapacity(packageWeights, daysAllowed); System.out.println("Minimum ship capacity: " + result); // Verification: capacity 11 means [3,2], [8], [4,7], [5,1,6] — 4 days ✓ // Another test case int[] smallBatch = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println("Minimum capacity for 5 days: " + minimumShipCapacity(smallBatch, 5)); // Answer: 15 (split: [1-5], [6-8], [9-10] ... verifiable) } }
Minimum capacity for 5 days: 15
| Pattern | When to Use It | Key Condition to Check | Gotcha |
|---|---|---|---|
| Classic Binary Search | Sorted array, find exact value | array[mid] == target | Must have no duplicates or don't care which occurrence you get |
| Left/Right Boundary Search | Sorted array with duplicates, find first/last position | Record mid, keep searching left (or right) | Must NOT return early when target found — keep going |
| Rotated Array Search | Array was sorted, then rotated at unknown pivot | Which half is cleanly sorted? Is target inside it? | Off-by-one in condition: use <= for left check, <= for right check |
| Answer-Space Binary Search | No array to search; answer range is monotonic | Is this candidate answer feasible? (Yes → go lower, No → go higher) | Hardest part is defining left/right bounds of answer space correctly |
🎯 Key Takeaways
- Every binary search variant shares one invariant: the answer lives inside [left, right]. Every boundary decision follows from that invariant — memorize the logic, not the code.
- For duplicate arrays, resist the urge to return early when you find the target. Record it as a candidate answer, then keep pushing the boundary in your search direction.
- In a rotated sorted array you don't need to find the pivot. Determine which half is cleanly sorted at every step — that's the only local information you need to make the right decision.
- Answer-space binary search is the most powerful and most overlooked pattern. If a problem says 'minimize the maximum' or 'maximize the minimum' with a feasibility check, binary search the answer range — not the input.
⚠ Common Mistakes to Avoid
- ✕Mistake 1: Using left < right instead of left <= right — This causes the loop to exit when left == right, skipping the last remaining candidate. The symptom is always returning -1 for single-element arrays or arrays where the target is at the final converged position. Fix: use while (left <= right) and trust that the body handles the single-element case correctly.
- ✕Mistake 2: Writing right = mid instead of right = mid - 1 when you've already confirmed array[mid] != target — This creates an infinite loop when left == mid (which happens when the search space is two elements). The loop never shrinks. Fix: always use right = mid - 1 and left = mid + 1 in the classic template, because you've already eliminated mid as a candidate.
- ✕Mistake 3: In answer-space binary search, setting the initial bounds wrong — For example, setting left = 0 for the ship capacity problem would include 0 as a candidate, which can never be valid. The feasibility function returns false but the real bug is the search never converges correctly. Fix: always derive left from the domain constraints (minimum meaningful answer) and right from the worst-case upper bound. State these derivations out loud during an interview.
Interview Questions on This Topic
- QGiven a sorted array rotated an unknown number of times and potentially containing duplicates, how would you find a target value — and what's the worst-case time complexity now that duplicates are allowed?
- QHow would you use binary search to find the square root of an integer N without using any built-in math functions, and how do you handle the case where N is not a perfect square?
- QYou have 1000 sorted files each containing 1 million log timestamps. How would you use binary search thinking to efficiently find all logs from a specific time range — and what data structures would you reach for?
Frequently Asked Questions
How do I know when to use binary search in a coding interview?
The clearest signal is a sorted input combined with an O(log n) time requirement. But also watch for the phrase 'minimize the maximum' or 'maximize the minimum' — those point to answer-space binary search even when the input isn't sorted. If you can verify whether a candidate answer is valid in O(n) time and the validity is monotonic (valid at X means valid at X+1), binary search the answer range.
What is the difference between left < right and left <= right in binary search?
Using left <= right means your loop processes the case where left == right — one candidate remaining. This is the correct condition for the classic find-exact-value template. Using left < right exits one iteration early and requires you to do a post-loop check on array[left]. Both can be made correct, but mixing them up is the root cause of most binary search bugs in interviews — pick one style and stick with it consistently.
Why does binary search only work on sorted arrays?
Binary search works by eliminating half the search space based on a comparison. That logic only holds if the data is ordered — knowing that array[mid] < target is useful only if you can guarantee all elements to the left of mid are also less than target. Without sorting, a smaller value at mid tells you nothing about where the target actually is. Answer-space binary search sidesteps this by searching a conceptual ordered range of answers, not the input data itself.
Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.