FoundationLesson 1 of 3 in Complexity AnalysisFree preview

Big O Notation

Big O describes how the work an algorithm does grows as the input size n grows. It is not a time in seconds.

Two rules turn an exact step count into Big O:

  1. Drop constants: 3n becomes O(n).
  2. Keep only the fastest-growing term: n² + 5n + 100 becomes O(n²).
Big ONameExample
O(1)constanta[i], HashMap get
O(log n)logarithmicbinary search
O(n)linearone loop over the array
O(n log n)linearithmicmerge sort
O(n²)quadratictwo nested loops
O(2ⁿ)exponentialnaive recursive Fibonacci
int count = 0;
for (int i = 1; i < n; i *= 2) count++; // i doubles: about log2(n) steps

The trap: constants still matter for small inputs. An algorithm doing 100n steps is slower than one doing n² steps until n passes 100. Big O only tells you who wins eventually.

In interviews: always state both time and space complexity of your solution without being asked, and say which term dominates.

Practice

Solve 3 of 4 to complete this lesson.

0/4 solved

Solve 3 more to complete this lesson, or move on and come back later.

Skip to next: Analysing Loops