Big O notation

How much does it grow? · Part of Complexity and data

Key idea

Practice

7 questions · about 4 min

Explained step by step

Big O

Big O notation describes how much an algorithm's work grows as the amount of data grows, called n. It doesn't measure seconds, but the shape of growth: if twice the data doubles the work, it's O(n). It looks at the worst case and ignores constants: 2n and n are both O(n). It's like comparing 2 recipes by how long it takes to cook for 10 or for 1,000 people, not by how long it takes today.

O(1), or constant time, means the work doesn't depend on how much data there is. Reading element 500 of a list by its position takes the same with 1,000 elements as with 1 million. Looking up a key in a hash table is also O(1) on average. It's like opening a locker knowing its number: it doesn't matter if there are 10 or 10,000 lockers.

O(n), or linear time, is when work grows in proportion to the data: twice the data, twice the work. It happens when going through every element 1 time, like summing a list or searching for a value one by one. With 1,000 elements it's 1,000 steps; with 1 million, 1 million. It's like reading a guest list to find a name: the more guests, the longer it takes.

O(n²), or quadratic time, shows up with 2 nested loops: for each element you go through all the others. With 10 elements it's 100 steps; with 1,000, 1 million; with 1 million, 1 trillion. That's why it quickly becomes impractical. It's like everyone at a party greeting everyone else: with 10 people it's quick, with 1,000 it lasts all night.

Sorted from best to worst: O(1) doesn't grow; O(log n) grows very slowly; O(n) grows just like the data; O(n log n), that of good sorting algorithms, a bit more; and O(n²) explodes. With 1 million items: 1, about 20, 1 million, about 20 million and 1 trillion steps. Picking the right complexity matters more than having a fast computer. It's like choosing between walking, biking, driving or flying: for short distances it doesn't matter, for long ones it does.

Binary search

Binary search finds a value in a sorted list by looking at the middle element: if what you want is larger, it discards the left half; if smaller, the right. It repeats until it finds it. It only works if the list is sorted, because otherwise you don't know which half to throw away. It's like looking up a word in a paper dictionary: you open it in the middle and keep discarding.

Splitting the data in half at each step gives O(log n), or logarithmic time: it grows very slowly. With 1,000 elements it's about 10 steps; with 1 million, about 20; with 1 billion, about 30. Doubling the data adds just 1 step. Binary search and balanced trees are O(log n). It's like the game of guessing a number from 1 to 1,000 with «higher» or «lower»: you get it in 10 questions.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

How much does it grow?