Binary Search is one of the most efficient algorithms for searching a sorted array or list. It offers a time complexity of O(log n), making it far faster than linear search algorithms, especially when working with large datasets. Understanding Binary Search is crucial for acing coding interviews, as it showcases your ability to work with sorted data and understand the fundamentals of divide and conquer techniques.
In this article, we will explore Binary Search in depth, covering the algorithm's workings, implementation, common variations, and tips and tricks to help you master this technique for coding interviews.
Binary Search is a divide and conquer algorithm that operates on sorted arrays or lists. Instead of checking every element sequentially, it reduces the search space by half at each step. By comparing the middle element of the array with the target, Binary Search efficiently narrows down the possible locations of the target.
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2 # Find the middle index
if arr[mid] == target:
return mid # Target found
elif arr[mid] < target:
left = mid + 1 # Search the right half
else:
right = mid - 1 # Search the left half
return -1 # Target not found
arr = [3, 9, 10, 27, 38, 43, 82]
target = 27
print(binary_search(arr, target)) # Output: 3
In this example, the target 27
is found at index 3
in the array.
Before diving into tips and tricks, let’s take a deeper look at some key concepts related to Binary Search:
mid = left + (right - left) // 2
Let’s now cover some tips and tricks that can help you become more efficient at implementing Binary Search and solve more complex problems during interviews.
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid # Target found
elif arr[mid] < target:
left = mid + 1 # Search the right half
else:
right = mid - 1 # Search the left half
return -1 # Target not found
Tip: Always ensure that the array is sorted before applying Binary Search, as applying it on unsorted data will yield incorrect results.
A more complex variant of Binary Search occurs when you need to search for an element in a rotated sorted array. A rotated sorted array is an array that was originally sorted, but then the elements have been shifted.
Given a rotated sorted array like [7, 9, 10, 27, 38, 43, 82, 3]
, you can still use Binary Search by adjusting the comparison logic.
You first check if the middle element is in the left sorted portion or the right sorted portion, then decide which half to search based on the comparison.
def rotated_binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
# Check if the left side is sorted
if arr[left] <= arr[mid]:
if arr[left] <= target < arr[mid]:
right = mid - 1 # Search left half
else:
left = mid + 1 # Search right half
else:
if arr[mid] < target <= arr[right]:
left = mid + 1 # Search right half
else:
right = mid - 1 # Search left half
return -1 # Target not found
Tip: If the array is rotated, use the additional logic to check if the current subarray is sorted. This ensures that Binary Search can still work efficiently.
In some interview questions, you may be required to find the first or last occurrence of a target in a sorted array. This can be efficiently handled by modifying the traditional Binary Search algorithm.
def find_first_occurrence(arr, target):
left, right = 0, len(arr) - 1
result = -1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
result = mid
right = mid - 1 # Search for earlier occurrences
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return result
This modification ensures that we continue searching the left side (i.e., earlier indices) even after finding the target.
In some coding interviews, you may be asked to perform Binary Search on infinite arrays or arrays with unknown length. You don’t know the exact number of elements, but you can perform searches using a dynamic range.
You can apply exponential search to find a suitable range in which the target might lie, and then apply Binary Search within that range.
def binary_search_in_infinite_array(arr, target):
left, right = 0, 1
# Find an upper bound by exponentially increasing right
while arr[right] < target:
left = right
right = 2 * right
# Apply binary search within the identified range
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # Target not found
In this case, the range is dynamically expanded to find where the target might be, and then Binary Search is applied within that range.
Always remember to consider edge cases in your Binary Search implementations. Here are a few to keep in mind:
Given an array of integers, find a peak element. An element is a peak if it is greater than its neighbors.
Solution: This problem can be solved using a modified version of Binary Search. At each step, you check if the middle element is greater than its neighbors. If it’s not, you move toward the larger side of the array.
def find_peak(arr):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if (mid == 0 or arr[mid - 1] <= arr[mid]) and (mid == len(arr) - 1 or arr[mid + 1] <= arr[mid]):
return mid
elif mid > 0 and arr[mid - 1] > arr[mid]:
right = mid - 1 # Move to the left side
else:
left = mid + 1 # Move to the right side
return -1
Binary Search is a powerful algorithm for efficiently searching in sorted arrays. Understanding its basic implementation and variations will make you well-prepared for coding interviews. Whether you're searching in a rotated sorted array, finding the first or last occurrence, or dealing with infinite arrays, Binary Search can be adapted to solve a wide range of problems.
Master these variations, handle edge cases carefully, and you'll have a strong toolset for tackling any search-related problem in coding interviews.
Login to Continue, We will bring you back to this content 0