

Lesson 01 · 14 min
Big O: how to estimate the speed of code
We count not seconds, but how the work grows with the data.
14 minLesson 1 of 8Practice includedAsk AI on any point
The same code runs at different speeds on a laptop and on a server. That is why algorithms are compared not in milliseconds but by how the number of steps grows as the input grows. The input size is called n: the length of an array, the number of users, the characters in a string.
Complexity classes you meet every day
O(1), constant:arr[i],map.get(key),stack.push(x). The time does not depend onn.O(log n), logarithmic: binary search. Each step discards half of the data, so a million elements take about 20 steps.O(n), linear: one pass over an array,indexOf,includes,filter.O(n log n): a good sort, includingarr.sort().O(n²), quadratic: nested loops over the same data. At 10,000 elements that is already 100 million steps.O(2ⁿ), exponential: trying every subset. Only fine for very smalln.
Complexity often hides in built-in methods. includes inside a loop looks like one line, but it is another pass on its own. Together that gives O(n²).
Memory counts too
Space complexity shows how much extra room an algorithm needs on top of the input itself. A few variables are O(1). A new array or Map with an entry per element is O(n).
- JavaScript in the knowledge base: the language of the course examples
- Python in the knowledge base: the same algorithms are often written in Python at interviews
- Resources: books and practice sites for algorithms
Check
Lock it in before you move on
1. A loop takes
3n + 10steps. What is the complexity?2. Two nested loops over one array of length n. What is the complexity?
3. Which algorithm runs in O(log n)?
4.
for (const x of a) if (b.includes(x)) count++, both arrays of length n. Complexity?5. A function creates a
Mapand puts every input element into it. What is the space complexity?