DSA Trainer
← Foundations

Foundations · Unit 1

Big-O Notation

You wrote two functions. They both return the right answer. One finishes instantly on a big input and the other hangs your laptop. Big-O is the language for explaining why, before you ever run the code.

Here is the part nobody tells you up front: Big-O is not about seconds. Two computers run at different speeds, so seconds are useless for comparing algorithms. Instead, Big-O counts how many steps your code takes as the input grows. A function that does one extra step per new item behaves very differently from one that does one extra step per pair of items, and Big-O is just a short label for that behavior.

Think about finding a name in a phone book. If you read every page from the front, doubling the size of the book doubles your work. That is linear, written O(n). If instead you open to the middle, decide which half the name is in, and throw the other half away, doubling the book only adds one extra step. That is logarithmic, written O(log n), and it is the reason binary search feels like magic.

You will mostly meet five of these: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), and O(n^2) quadratic. The unit below walks you through all five on real code, with a checkpoint after every idea so you predict first and find out second. That prediction step is the whole reason it sticks instead of sliding off.

Goal: Given a short piece of code, state its time complexity and explain why, out loud, without guessing.
Lesson 1 of 60%

You’re not signed in. Your progress here won’t be saved.

Why this exists

Here is a problem you have already felt. You have two functions that both check whether a list contains any duplicate values. They return the exact same answer on every input. One of them is fine on a million items. The other one freezes. From the outside, the code looks similar. So what is actually different?

The difference is not the language, the laptop, or the answer. It is how the amount of work grows as the list gets bigger. That growth is the only thing Big-O measures, and it is the thing that decides whether your code survives real input.

You time a function on 1,000 items and it takes 1 second. On 2,000 items it takes 4 seconds. If you double the input again to 4,000 items, what is your best guess for the time?