01The idea
Big-O describes how the work of a solution grows when the input grows. It does not count seconds. It counts steps, and it only keeps the part that grows fastest.
Say a solution takes 3n + 5 steps on n items. When n is large, the 5 matters little, and the 3 does not change the shape of the growth. We drop both and write O(n).
These are the shapes you will meet most often, from fastest to slowest:
| Big-O | Name | Typical code |
|---|---|---|
| O(1) | constant | one hash map lookup, one array read |
| O(log n) | logarithmic | cut the input in half each step |
| O(n) | linear | one loop over the input |
| O(n log n) | n log n | sort the input |
| O(n²) | quadratic | a loop inside a loop |
| O(2ⁿ) | exponential | try every subset |
Space works the same way. Count the extra memory the solution uses as the input grows. A few variables are O(1). A hash map that holds every item is O(n).
02How to spot it
You read the Big-O from the shape of the code:
- One loop over n items is O(n).
- A loop inside a loop, both over the items, is O(n²).
- A loop that halves what is left each time is O(log n).
- Two loops one after the other add up. O(n) + O(n) is still O(n).
- Sorting first costs O(n log n). Add everything after the sort to that.
Use the input limits in the problem to pick a target. A computer does about 10⁸ simple steps per second. If n can be 100,000, then n² is 10¹⁰ steps, which is far too slow. You need O(n log n) or better. If n is at most 20, an O(2ⁿ) search over every subset can still pass.
03Worked example
The template counts the steps of three loop shapes on the same n items and returns the three counts.
The input is n = 8.
The first loop visits each item once, so it counts 8 steps.
The second loop pairs each item with every item after it. Here is the pair count after each item of the outer loop:
| i | items after i | pairs so far |
|---|---|---|
| 0 | 7 | 7 |
| 1 | 6 | 13 |
| 2 | 5 | 18 |
| 3 | 4 | 22 |
| 4 | 3 | 25 |
| 5 | 2 | 27 |
| 6 | 1 | 28 |
| 7 | 0 | 28 |
The third loop halves the size until one item is left. The size goes 8, 4, 2, 1. That is 3 halvings.
The function returns [8, 28, 3].
Now run it with n = 16. It returns [16, 120, 4]. Compare the two runs:
| Shape | n = 8 | n = 16 | What doubling n did |
|---|---|---|---|
| one loop | 8 | 16 | doubled the steps |
| loop in a loop | 28 | 120 | about four times the steps |
| halving | 3 | 4 | added one step |
This is what Big-O predicts. O(n) doubles, O(n²) grows four times, and O(log n) adds one step.
04Watch it run
The worked example, step by step, with the code line that runs.
05Complexity
The template runs all three loops one after the other. The loop in a loop is the slowest part, so the whole function is O(n²) time. When parts run one after the other, the Big-O is the slowest part.
It uses O(1) extra space. It keeps three counters and a size, however large n gets.
06Common mistakes
- Keeping constants. O(2n) and O(n + 10) are both O(n). Write the simplest form.
- Missing a hidden loop.
x in my_list,my_list.index(x)and slicing all walk the list, so each one costs O(n). Inside another loop they make the solution O(n²). - Adding when you should multiply. A loop inside a loop multiplies the counts. Two loops one after the other add them.
- Forgetting the sort. If the solution sorts, it is at least O(n log n), even when the rest is O(n).
- Ignoring space. Recursion uses stack space for every open call. A recursion that goes n calls deep uses O(n) space.
07Code template
def count_steps(n):
linear = 0
for i in range(n): # one loop: n steps
linear += 1
pairs = 0
for i in range(n): # a loop inside a loop: about n * n / 2 steps
for j in range(i + 1, n):
pairs += 1
halvings = 0
size = n
while size > 1: # cut the size in half each time: about log2(n) steps
size //= 2
halvings += 1
return [linear, pairs, halvings]
if __name__ == "__main__":
print(count_steps(8))
print(count_steps(16))
import java.util.*;
public class Solution {
static int[] countSteps(int n) {
int linear = 0;
for (int i = 0; i < n; i++) { // one loop: n steps
linear++;
}
int pairs = 0;
for (int i = 0; i < n; i++) { // a loop inside a loop: about n * n / 2 steps
for (int j = i + 1; j < n; j++) {
pairs++;
}
}
int halvings = 0;
int size = n;
while (size > 1) { // cut the size in half each time: about log2(n) steps
size /= 2;
halvings++;
}
return new int[]{linear, pairs, halvings};
}
public static void main(String[] args) {
System.out.println(Arrays.toString(countSteps(8)));
System.out.println(Arrays.toString(countSteps(16)));
}
}
08Check yourself
Get every answer right to mark this lesson as done in all your study plans.
Your result is saved and marks this lesson as done in your study plans.