Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The Binary Search Patterns pattern applies when you need standard binary search, rotated arrays, first/last occurrence, and search on answer space. Its time complexity is O(log n) and space complexity O(1). It is used in 26 problems on Thita, including Binary Search, Capacity To Ship Packages Within D Days and Find First and Last Position of Element in Sorted Array. Common variations are On Sorted Array/List and Find First/Last Occurrence.
Master binary search algorithms including standard search, rotated array search, search on answer space, and finding boundaries. Essential for efficient O(log n) solutions.
Binary Search Patterns is pattern 3 of 16 in the Thita.ai DSA course. It covers standard binary search, rotated arrays, first/last occurrence, and search on answer space, split into 5 subpatterns and about 2.5 hours of study. It is one of the interview-critical patterns, so it is worth covering before your first onsite.
Each subpattern below has its own theory page and problem set. They are listed in the order the course teaches them.
Read the theory for a subpattern, then solve its problems in the browser editor. Your solution runs against the problem's test cases, and the AI coach gives a hint on the technique you are missing rather than the finished answer. Progress is tracked per subpattern, so the tracker shows which of the 5 Binary Search Patterns subpatterns you have covered. Topics covered here include binary search, binary search algorithm, search sorted array, rotated sorted array, binary search variations, search insert position, first last occurrence, binary search on answer.
26 problems, easiest first.
Imagine you’re searching for a word in a dictionary. Instead of flipping through every single page, you open the book roughly in the middle, see where you are, and decide whether to look in the first half or the second half. This process of repeatedly dividing your search space in half is the essence of binary search.
The "Binary Search on Sorted Array/List" subpattern is all about efficiently finding information in data that’s already sorted. Whether you’re looking for a specific number, the place to insert a new value, or the first occurrence of an error, this pattern helps you zero in on the answer much faster than checking every element one by one.
When is this pattern useful?
Why use it?
Let’s break down what makes binary search so powerful and how it works:
left at the beginning and right at the end of your array.mid = left + (right - left) / 2 to avoid overflow.Imagine a row of lockers numbered 1 to 100, and you’re looking for locker 73. Instead of opening each locker one by one, you start at locker 50. If 73 is higher, you jump to locker 75, then maybe to 62, and so on—always halving your search space.
Binary search leverages the properties of sorted data and logarithms. Each time you halve the search space, you’re essentially asking, "How many times can I divide n by 2 before I get to 1?"—which is log₂(n).
Let’s build a mental model for tackling binary search problems on sorted arrays/lists:
When the target is missing, the halving loop still ends in the right place: left passes right, and left is the index where the target would be inserted. Here 5 would go at index 2.
left and right pointers to cover the entire array.Suppose you’re searching for 5 in [1, 3, 7, 9]. Since 5 isn’t present, where would you insert it? Between 3 and 7. Your binary search should return the index where 5 would fit.
while (left <= right) is standard for searching for an exact match.while (left < right).left, right, mid, target.Let’s look at two classic problems from the database: Binary Search and Search Insert Position.
1// Problem: Binary Search
2// Given a sorted array 'nums' and a target value, return the index if found, else -1.
3
4int binarySearch(const vector<int>& nums, int target) {
5 int left = 0; // Start of the array
6 int right = nums.size() - 1; // End of the array
7
8 while (left <= right) { // While there is a search space
9 int mid = left + (right - left) / 2; // Find the middle (avoids overflow)
10
11 // Check if the middle element is the target
12 if (nums[mid] == target) {
13 return mid; // Found the target!
14 }
15 else if (nums[mid] < target) {
16 left = mid + 1; // Target is in the right half
17 }
18 else {
19 right = mid - 1; // Target is in the left half
20 }
21 }
22 return -1; // Target not found
23}Explanation:
left and right.1// Problem: Search Insert Position
2// Find the index where the target should be inserted in a sorted array.
3
4int searchInsert(const vector<int>& nums, int target) {
5 int left = 0;
6 int right = nums.size() - 1;
7
8 while (left <= right) {
9 int mid = left + (right - left) / 2;
10
11 if (nums[mid] == target) {
12 return mid; // Target found at mid
13 }
14 else if (nums[mid] < target) {
15 left = mid + 1; // Move right
16 }
17 else {
18 right = mid - 1; // Move left
19 }
20 }
21 // At the end, 'left' is the correct insertion index
22 return left;
23}Explanation:
left will be at the position where it should be inserted to keep the array sorted.Let’s think about this step by step:
Problem Statement:
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if inserted in order.
Step 1: Intuition
Imagine you’re organizing books on a shelf in alphabetical order. If you get a new book, you want to put it in the right spot so the order is maintained. You don’t want to scan every book; instead, you can use binary search to quickly find the correct position.
Step 2: Approach
left at 0, right at the end of the array.left <= right, find the mid index.nums[mid] is the target, return mid.nums[mid] is less than the target, move left to mid + 1.nums[mid] is greater, move right to mid - 1.left will be at the position where the target should be inserted.Step 3: Why does this work?
Because the array is sorted, every time you eliminate half the search space, you’re guaranteed not to miss the correct position. The final value of left is the smallest index where nums[left] >= target.
Step 4: Edge Cases
left will be 0.left will be nums.size().Step 5: Connecting Back
This is a classic binary search application—using the sorted property to efficiently find not just a value, but also its correct position.
Signals to Use This Pattern:
Aha! Moments:
left) can give you insertion points or boundaries.Debugging Tips:
left, right, and mid values during debugging to see how the search space changes.Memory Tricks:
Keep practicing, and soon binary search will feel as natural as flipping to the right page in a book!