Algorithms and Data Structures

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.

AI

You can ask about this rule«Big O describes growth, not exact time»

One pass: O(n)
A loop inside a loop: O(n²)
AI

You can ask about this point«Lesson summary»

Complexity classes you meet every day

  • O(1), constant: arr[i], map.get(key), stack.push(x). The time does not depend on n.
  • 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, including arr.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 small n.
AI

You can ask about this rule«Worst case by default»

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²).

The square you do not see
AI

You can ask about this point«Complexity classes you meet every day»

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).

Same result, different memory
AI

You can ask about this rule«Time is often traded for memory»

AI

You can ask about this point«Memory counts too»

Check

Lock it in before you move on

  1. 1. A loop takes 3n + 10 steps. What is the complexity?

  2. 2. Two nested loops over one array of length n. What is the complexity?

  3. 3. Which algorithm runs in O(log n)?

  4. 4. for (const x of a) if (b.includes(x)) count++, both arrays of length n. Complexity?

  5. 5. A function creates a Map and puts every input element into it. What is the space complexity?

PracticeSolve the lesson tasksA few short tasks in the editor. Check your code right away and see where it goes wrong.Start practice