Lesson 15 of 30
Binary search explained through the guess-the-number-between-1-and-100 game, and how each guess cuts the remaining possibilities in half.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
You've probably played this game: someone picks a number between 1 and 100, and you have to guess it. After each guess, they tell you "higher" or "lower." What's your best first guess?
Most people's instinct, once they think about it, is 50 — not 1, not 99. Why? Because 50 sits exactly in the middle. If they say "higher," you've just thrown out numbers 1 through 50 in one move — half the board is gone, from a single guess. If they say "lower," you threw out 51 through 100 instead. Either way, half the possibilities vanish.
Now you guess again, right in the middle of whatever's left — say 75, if the answer was "higher" than 50. Same trick: half of what remained is gone. Guess by guess, the pile of possible numbers keeps cutting in half, until there's only one number left standing.
That's binary search. "Binary" just means "splitting into two," which is exactly what happens every time you guess the middle: the remaining possibilities split into a half you keep and a half you throw away.
Here's the same idea, done on a sorted array instead of "1 to 100" — searching for a target value by repeatedly checking the middle.
function binarySearch(sortedArr, target) {
let low = 0;
let high = sortedArr.length - 1;
while (low <= high) {
// Check the middle of what's left, not the middle of the whole array.
const mid = Math.floor((low + high) / 2);
if (sortedArr[mid] === target) {
return mid; // found it
} else if (sortedArr[mid] < target) {
low = mid + 1; // target is bigger — throw away the lower half
} else {
high = mid - 1; // target is smaller — throw away the upper half
}
}
return -1; // not in the array
}
console.log(binarySearch([2, 5, 8, 12, 16, 23, 38, 56, 72, 91], 23)); // 5
Trace it: low is 0, high is 9, so mid is 4 — that's 16. 16 < 23, so throw away everything at or below index 4; low becomes 5. Now mid is 7 — that's 56. 56 > 23, so throw away everything at or above index 7; high becomes 6. Now mid is 5 — that's 23. Found it, in 3 guesses, out of 10 numbers.
low and high. Forgetting to move low or high past the middle you already checked can make the search loop forever or skip the answer. This is a normal, extremely common bug to hit the first several times you write this — not a sign you don't understand the idea.This is the exact same trick as flipping to the middle of a phone book to find a name, then flipping to the middle of whichever half is left — the phone-book picture used earlier in this track to explain why some searches are fast. Binary search is that same halving instinct, written out as code so a computer can do the flipping for you, guess after guess, automatically.