1. Introduction to Binary Search Interview Questions
Binary Search is one of the most efficient techniques in computer science, halving the search space at each step. Below are 2 essential Binary Search questions with full implementations in C++, Java, Python, and JavaScript.
2. Core Binary Search Questions
Q1. Classic Binary Search
Keep an inclusive interval [left, right] containing every position that could still match the target. At each step, compare the middle value with the target. In an ascending sorted array, if the middle value is too small, every value at or before it is too small as well; set left = mid + 1. If it is too large, set right = mid - 1. Both updates discard the middle position, guaranteeing progress.
For [2, 5, 8, 12, 16] and target 12, the first middle index is 2 (value 8). Search indices 3 through 4 next; index 3 matches. For target 9, neither remaining value matches and the interval eventually becomes empty. The result is -1, not the insertion position.
An empty array should return -1. With duplicate values, this version returns a matching index, not necessarily the first occurrence. To find the first occurrence, record a match and keep searching to its left. The sorted-input requirement is essential: applying this code to an arbitrary array gives no correctness guarantee.
Question: Given a sorted array of integers nums and a target, return its index or -1.
cppint search(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
javapublic int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
pythondef search(nums: list, target: int) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return -1
javascriptfunction search(nums, target) { let left = 0, right = nums.length - 1; while (left <= right) { let mid = Math.floor(left + (right - left) / 2); if (nums[mid] === target) return mid; else if (nums[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
Time Complexity: O(log n) | Space Complexity: O(1)
Q2. Search in Rotated Sorted Array
Assume the original sorted array contains distinct values and was rotated at one position. At least one half of the current interval remains sorted. If the left half is sorted, check whether the target lies between its first value and the middle value. Keep that half only when the target is in its range; otherwise keep the right half. Apply the symmetric check when the right half is sorted.
For [4, 5, 6, 7, 0, 1, 2] and target 0, the first middle value is 7. The left half [4, 5, 6, 7] is sorted, but cannot contain 0, so discard it. In the remaining interval [0, 1, 2], normal range comparisons lead to index 4 in the original array.
Distinct values matter. With duplicates such as [1, 0, 1, 1, 1], equal endpoints can hide the rotation point. A duplicate-aware variant may need to shrink equal boundaries one element at a time and can take O(n) in the worst case. Do not describe this implementation as solving that different problem.
cppint searchRotated(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; if (nums[left] <= nums[mid]) { if (nums[left] <= target && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { if (nums[mid] < target && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return -1; }
javapublic int searchRotated(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; if (nums[left] <= nums[mid]) { if (nums[left] <= target && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { if (nums[mid] < target && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return -1; }
pythondef searchRotated(nums: list, target: int) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1
javascriptfunction searchRotated(nums, target) { let left = 0, right = nums.length - 1; while (left <= right) { let mid = Math.floor(left + (right - left) / 2); if (nums[mid] === target) return mid; if (nums[left] <= nums[mid]) { if (nums[left] <= target && target < nums[mid]) right = mid - 1; else left = mid + 1; } else { if (nums[mid] < target && target <= nums[right]) left = mid + 1; else right = mid - 1; } } return -1; }
Time Complexity: O(log n) | Space Complexity: O(1)
3. Summary Table
| Problem | Pattern | Time | Space |
|---|---|---|---|
| Binary Search | Standard Halving | O(log n) | O(1) |
| Search Rotated | Half-Sorted Detection | O(log n) | O(1) |
Practice all binary search problems on DSAMaster's practice platform.
