Binary Search Template: Pattern, Code & Cheat Sheet
The Binary Search pattern is one of the most frequently tested coding interview patterns. Divide search space in half for logarithmic time lookups. This template gives you a reusable code skeleton, pseudocode, and implementation in multiple languages so you can solve 14+ problems using this single mental model.
Difficulty: Easy | Time Complexity: O(log n) | Space Complexity: O(1)
When to Use This Template
Use the Binary Search template when you see these signals in a problem:
Prerequisites: Arrays basics, Sorting
Problem count on W Code: 14 problems across Easy, Medium, and Hard difficulty levels.
If the problem does not match these signals, consider alternative patterns.
Pseudocode Template
function binarySearch(arr, target):
left = 0
right = length(arr) - 1
while left <= right:
mid = left + (right - left) / 2
if arr[mid] == target:
return mid
else if arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1Python Implementation
pythondef binary_search(arr: list, target: int) -> int: left, right = 0, len(arr) - 1 while left <= right: mid = left + (right - left) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1
Java Implementation
javapublic int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
C++ Implementation
cppint binarySearch(vector<int>& arr, int target) { int left = 0, right = arr.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
Variations & Adaptations
The Binary Search pattern has several variations you should master:
Variation 1: Classic Search
This variation is useful when the problem specifically requires classic search. Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 2: Lower Bound / Upper Bound
This variation is useful when the problem specifically requires lower bound / upper bound. Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 3: Search in Rotated Array
This variation is useful when the problem specifically requires search in rotated array. Adapt the main template by modifying the core loop/recursion logic accordingly.
Variation 4: Binary Search on Answer
This variation is useful when the problem specifically requires binary search on answer. Adapt the main template by modifying the core loop/recursion logic accordingly.
Common Mistakes & Edge Cases
When implementing Binary Search, watch out for:
Edge cases to always test:
Step-by-Step Problem Solving Guide
Frequently Asked Questions
What problems can I solve with the Binary Search template?
What is the time complexity of Binary Search?
What should I learn before Binary Search?
How do I recognize a Binary Search problem in an interview?
Practice 14+ Binary Search problems on W Code with instant feedback and AI-powered hints. Start your free practice now!
Start Learning Free