Lesson 2 of 30
Big O is just a way of describing how much MORE work a task takes as the input gets bigger. No formulas required to get the idea.
12 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
"Big O" is one of the phrases that scares people away from this whole topic, so let's take it apart slowly. By the end of this lesson, it will feel like common sense, because it already is common sense — it just got a math name stapled onto it.
Picture two ways of finding a name in a phone book. The first way: open to a random page, and if the name you want comes later in the alphabet, flip forward; if it comes earlier, flip back; keep narrowing the gap. You'd find almost any name in ten flips or fewer, even in a book with a thousand pages. The second way: start at page one, and read every single name until you find the one you want. That could take you all the way to the last page.
Both ways work. But they don't get slower at the same rate when the phone book gets bigger. Double the pages, and flipping-and-narrowing barely notices — maybe one extra flip. Double the pages, and reading-every-name takes roughly twice as long. That difference — not the exact number of steps, but how the number of steps grows as the input grows — is the entire idea behind Big O.
Big O is a way of describing that growth pattern with a short label, so programmers can talk about it without re-explaining it every time. "Reading every name" is O(n): the work grows in a straight line with the size of the input. "Flipping and narrowing" is O(log n): the work barely grows at all, even as the input gets huge. You'll meet the exact vocabulary for these labels in the next lesson — for now, the only thing to hold onto is: Big O answers "how does the work grow?", not "exactly how many steps is that?"
// Way 1: check every name, one at a time, until we find it (or don't).
// This is the "read every page" approach.
function findLinear(names, target) {
for (let i = 0; i < names.length; i++) {
if (names[i] === target) return i; // found it, stop here
}
return -1; // never found it
}
// Way 2: the list is already sorted alphabetically, so we can
// narrow the search in half each time, like flipping a phone book.
function findByNarrowing(sortedNames, target) {
let low = 0;
let high = sortedNames.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2); // check the middle
if (sortedNames[mid] === target) return mid; // found it
if (sortedNames[mid] < target) low = mid + 1; // target is later, narrow up
else high = mid - 1; // target is earlier, narrow down
}
return -1; // never found it
}
This is the same idea you'll use to judge almost every solution for the rest of this track, and in interviews well beyond it. The next lesson takes this same phone-book intuition and connects it to specific labels — O(1), O(n), O(n^2), O(log n) — so you can recognize them by sight.