Binary Search for Beginners: The One Idea You Actually Need to Understand
Binary Search is one of those algorithms that looks very easy when someone explains it.

Binary Search for Beginners: The One Idea You Actually Need to Understand
You have a sorted array. Find the middle. Go left or right. Done.
But then you see an interview problem like:
“Find the minimum possible capacity such that all packages can be shipped within D days.”
And suddenly you wonder:
“Where is the sorted array?”
This is where Binary Search becomes interesting.
The real skill is not memorizing the Binary Search code.
It is learning to recognize:
“I have a search space, and I can eliminate a large part of it.”
Once you understand that idea, many different Binary Search problems start looking similar.
1. What Is Binary Search?
Let’s start with a simple example.
Suppose we have a sorted array:
[2, 5, 8, 12, 16, 23, 38]We want to find 23.
The obvious approach is to check every element:
2 → 5 → 8 → 12 → 16 → 23This works, but we are doing unnecessary work.
Because the array is sorted, we can do something smarter.
Look at the middle:
[2, 5, 8, 12, 16, 23, 38]
↑
12Our target is 23.
We know:
23 > 12Because the array is sorted, everything to the left of 12 is also smaller than 23.
So we can completely ignore it.
Now our search space is:
[16, 23, 38]Again, check the middle.
[16, 23, 38]
↑
23Found it.
That’s Binary Search.
The entire idea in one sentence
Use information about the middle of the search space to eliminate a large part of the search space.
For a sorted array, we can usually eliminate half of the remaining elements at every step.
That’s why the time complexity is:
O(log n)Compare that with a normal linear search:
O(n)For a very large array, that difference becomes significant.
2. The Basic Binary Search
The classic problem is:
Given a sorted array, find the index of a target element. Return
-1if it doesn't exist.
For example:
A = [2, 5, 8, 12, 16, 23, 38]
target = 16We maintain three variables:
start
mid
endInitially:
start = 0
end = A.length - 1Then:
mid = Math.floor((start + end) / 2)There are only three possibilities.
Case 1: We found the target
A[mid] === targetReturn mid.
Case 2: Target is on the right
target > A[mid]Everything from start through mid can be ignored.
So:
start = mid + 1Case 3: Target is on the left
target < A[mid]Everything from mid through end can be ignored.
So:
end = mid - 1The JavaScript implementation looks like this:
function binarySearch(A, target) {
let start = 0;
let end = A.length - 1;
while (start <= end) {
const mid = Math.floor((start + end) / 2);
if (A[mid] === target) {
return mid;
}
if (target > A[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}The important part isn’t the code.
It’s the decision:
Compare target with A[mid]
A[mid]
/ \
left right
↑ ↑
target targetEvery comparison tells us which half we can throw away.
3. Why Is It O(log n)?
Suppose you have: 1,000,000 elements
With linear search, you could potentially inspect all: 1,000,000 elements.
Binary Search roughly does:
1,000,000
↓
500,000
↓
250,000
↓
125,000
↓
...
↓
1We keep dividing the search space by 2.
That’s what logarithmic complexity represents.
So:
Time: O(log n)
Space: O(1)for the iterative version.
4. Iterative vs Recursive Binary Search
There are two common ways to write Binary Search.
Iterative
We use a loop:
while (start <= end) {
// binary search
}This is the version you’ll probably use most often.
Recursive
We can also call the same function again on the smaller search space:
function binarySearch(A, target, start, end) {
if (start > end) {
return -1;
}
const mid = Math.floor((start + end) / 2);
if (A[mid] === target) {
return mid;
}
if (target > A[mid]) {
return binarySearch(A, target, mid + 1, end);
}
return binarySearch(A, target, start, mid - 1);
}The algorithm hasn’t changed.
We’re still doing:
Find middle
↓
Compare
↓
Discard half
↓
RepeatThe only difference is how we repeat the process.

Time complexity Iterative/Recursive
For interviews, it’s useful to understand both, even if you prefer the iterative version.
5. The Part Most Beginners Miss: Recognising Binary Search
This is where Binary Search becomes much more useful.
A common beginner rule is:
“If the array is sorted, use Binary Search.”
That’s correct, but incomplete.
There are many Binary Search problems where the array isn’t obviously sorted.
Instead, look for a search space that has some kind of monotonic behavior.
Here are the patterns I look for.
Pattern 1: The Search Space Is Sorted
The easiest case.
[1, 4, 7, 10, 15, 20]Question:
Find
15.
We know that if:
A[mid] < targetthe left side cannot contain the answer.
So we can discard it.
Classic Binary Search.
Practice
Start with:
Binary Search
Search Insert Position
Find Floor
Find Ceiling
The goal here is simply to become comfortable with:
start
mid
endPattern 2: Find the First or Last Occurrence
Now consider:
[1, 2, 2, 2, 3, 4]Suppose the question is:
Find the first occurrence of
2.
You perform Binary Search and find:
2
↑Should you immediately return? No.
There could be another 2 on the left.
So we record the answer and continue searching left:
answer = mid
end = mid - 1Similarly, for the last occurrence, after finding 2, we continue searching right:
answer = mid
start = mid + 1This is a very important shift in thinking.
Normal Binary Search asks:
“Did I find the target?”
Modified Binary Search asks:
“I found a valid answer. Can I find an even better one?”
This pattern leads to:
First occurrence
Last occurrence
Count occurrences
Lower Bound
Upper Bound
Pattern 3: Find a Boundary
Consider this array:
[false, false, false, true, true, true]The question is:
Where does
truestart?
We aren’t searching for a particular number.
We’re searching for a transition:
FALSE FALSE FALSE | TRUE TRUE TRUE
↑
answerThis is a Binary Search pattern.
Why?
Because the values have a monotonic structure:
false → false → false → true → true → trueOnce we reach true, everything after it is also true.
So when we find a true, we can ask:
“Could there be an earlier true?”
If yes, search left.
This same idea appears in many problems involving:
First valid position
First invalid position
Minimum satisfying value
Transition points
Lower/upper bounds
A useful mental model is:
Binary Search often finds the point where something changes.
Pattern 4: Binary Search on the Answer
This is probably the most confusing Binary Search pattern for beginners.
Imagine a problem asks:
What is the minimum value of
Xsuch that some condition becomes possible?
There may be no sorted array.
Instead, imagine the possible answers are:
X: 1 2 3 4 5 6 7 8 9
possible: N N N N Y Y Y Y YLook at the structure:
NO NO NO NO | YES YES YES YES YES
↑
answerThat’s a Binary Search problem.
We can search the answer space.
Pick the middle value:
mid = 5Ask:
Is
5possible?
If yes, maybe we can do even better.
Search left.
If no, we need a larger answer.
Search right.
The general structure becomes:
Possible answer range
↓
Pick middle
↓
Can this answer work?
/ \
YES NO
↓ ↓
search search
left rightThe critical question is:
Can I efficiently check whether a candidate answer is possible?
And another critical requirement is:
Does the answer become consistently possible or impossible as I move through the search space?
If yes, Binary Search may be hiding inside the problem.
This pattern appears in problems such as:
Book Allocation
Painter’s Partition
Aggressive Cows
Capacity to Ship Packages Within D Days
Koko Eating Bananas
These problems look completely different from:
find target in sorted arrayBut underneath, they use the same idea:
Search a space and eliminate half of it using a monotonic condition.
Pattern 5: Rotated Sorted Arrays
Now consider:
[4, 5, 6, 7, 0, 1, 2]The array isn’t completely sorted.
So can we still use Binary Search?
Yes.
At first this feels strange.
But look at the middle:
[4, 5, 6, 7, 0, 1, 2]
↑
7At least one side around the middle will have useful sorted structure.
For example:
[4, 5, 6, 7]is sorted.
That gives us information.
We can determine:
Is the target inside this sorted half?
If yes, search there.
If not, search the other half.
This leads to problems such as:
Search in Rotated Sorted Array
Find Minimum in Rotated Sorted Array
Search in Rotated Sorted Array II
The important lesson is not the exact code.
It’s this:
Binary Search doesn’t always require the entire array to be perfectly sorted.
Sometimes we only need enough structure to determine:
Which part can safely be discarded?
6. A Binary Search Learning Path
If you’re learning Binary Search for interviews, don’t randomly solve 20 problems.
Build the pattern gradually.
Level 1 — Classic Binary Search
Start with:
Binary Search
Search Insert Position
Find Floor
Find Ceiling
Goal
Become comfortable with:
start
mid
endand understand why one half can be discarded.
Level 2 — First / Last Position
Then move to:
First occurrence
Last occurrence
Count occurrences
Lower Bound
Upper Bound
Goal
Learn that finding a valid answer doesn’t always mean you should stop.
Sometimes you need to keep searching for a better boundary.
Level 3 — Boundaries
Now practice problems where the search space looks like:
FALSE FALSE FALSE TRUE TRUE TRUEExamples include:
First Bad Version
First valid position
Minimum value satisfying a condition
Transition point problems
Goal
Stop thinking:
“I’m searching for a number.”
Start thinking:
“I’m searching for where something changes.”
Level 4 — Rotated / Modified Arrays
Practice:
Search in Rotated Sorted Array
Find Minimum in Rotated Sorted Array
Search in Rotated Sorted Array II
Goal
Learn how to inspect the middle and determine which half has useful information.
Level 5 — Binary Search on Answer
Finally, move to:
Book Allocation
Painter’s Partition
Aggressive Cows
Capacity to Ship Packages Within D Days
Koko Eating Bananas
Goal
Recognise that your search space doesn’t have to be an array.
It can simply be:
minimum possible answer
↓
...
↓
maximum possible answerAnd for every candidate, you ask:
Is this possible?7. The Binary Search Checklist
When you get a new interview problem, don’t immediately start writing:
let start = 0;
let end = n - 1;First ask yourself a few questions.
1. Is there some ordering or monotonic behavior?
Maybe the array is sorted.
Or maybe a condition changes only once:
false false false true true true2. Can I eliminate a large part of the search space?
If knowing something about the middle lets you discard half of the possibilities, Binary Search is worth investigating.
3. Can I determine which side is useless?
This is the heart of Binary Search.
Ask:
“After looking at the middle, can I confidently throw away one side?”
If yes, you’re getting closer.
4. Am I looking for a boundary?
For example:
NO NO NO NO | YES YES YESMaybe the answer is exactly at the transition.
5. Am I looking for the minimum or maximum value that satisfies a condition?
This is a major clue for Binary Search on Answer.
6. Can I efficiently check whether a candidate answer is possible?
If you can do:
isPossible(mid)and the result behaves monotonically, Binary Search might be the right tool.
8. The One Idea to Remember
When people learn Binary Search, they often remember this:
while (start <= end) {
const mid = ...
}But that’s not the important part.
The important part is the thinking behind it.
Binary Search is not really about arrays.
It is about reducing a search space.
The pattern is:
Search Space
↓
Pick a middle
↓
Learn something
↓
Discard a large part
↓
RepeatFor a sorted array, the search space is the array.
For a rotated array, it’s still the array, but we use its partial ordering.
For first/last occurrence, we’re searching for a boundary.
For problems like Book Allocation or Koko Eating Bananas, the search space is the possible answer itself.
That’s why Binary Search is much bigger than the basic:
“Find a number in a sorted array.”
Once you start seeing problems as:
search space + useful information + monotonic behavior + boundaries
Binary Search stops looking like a trick you need to memorize.
It starts looking like a pattern you can recognise.
And that is the real Binary Search skill.