Guess the Big O

Read the snippet. Pick its time complexity before the bar runs out. Chain correct answers to multiply your score. 66 snippets in C++, Java, Python and Rust — or skip to the complexity cheat sheet.

10 secondsAnswer fast for a bigger speed bonus. The clock tightens as you go.
Combo multiplierEvery 3 in a row raises your multiplier, up to 5x.
3 livesA wrong answer or a timeout costs one. Survive as long as you can.
0
C++
Pick the complexity that matches the code.
Run over
0
0
Correct
0
Best combo
0
Your best

High scores

Playing also counts toward your daily study time and streak.

Loading…

Big O complexity cheat sheet

Big O notation describes how the work an algorithm does grows as its input grows. It drops constants and keeps only the dominant term, so 3n + 5 steps is simply O(n). The practice game above draws from 66 snippets covering all nine classes below.

ComplexityNameDoubling the input…Typical exampleSteps at n = 1,000
O(1)Constantchanges nothingHash table lookup, array index1
O(log n)Logarithmicadds one stepBinary search, balanced tree lookup~10
O(√n)Rootmultiplies by 1.41Trial division primality test~32
O(n)Lineardoubles the workOne loop over the input, linear search1,000
O(n log n)Linearithmicslightly more than doublesMerge sort, heap sort, most good sorts~10,000
O(n²)Quadraticquadruples the workNested loops, bubble sort1,000,000
O(n³)Cubicmultiplies by 8Triple loop, naive matrix multiply1,000,000,000
O(2ⁿ)Exponentialsquares the workRecursive Fibonacci without memoisationastronomical
O(n!)Factorialworse than squares itBrute force permutations, naive TSPastronomical

Step counts are rough orders of magnitude, not exact operation counts. Anything below the O(n²) line stops being usable well before a million items.

How to work out time complexity

Most interview snippets give up their complexity to five rules. The game rewards recognising these on sight, because you only get ten seconds.

  1. Count loops over the input, not the work inside them. A loop body doing 50 arithmetic operations is still O(n) — constants drop out.
  2. Nested loops multiply; sequential loops add. Two nested loops over the same input are O(n²). Two loops one after the other are O(n) + O(n) = O(n), because you keep the largest term.
  3. Halving the range each step is O(log n). Anything that discards half the remaining data per iteration — binary search, or while (n > 1) n /= 2 — is logarithmic.
  4. Divide and conquer that touches every element per level is O(n log n). Merge sort splits log n times and does O(n) merging work at each level.
  5. For recursion, think branches to the power of depth. Two recursive calls per step with depth n gives O(2ⁿ). Memoising collapses the repeated work, usually back to O(n).

The trap interviewers set most often: a loop that looks nested but isn't. Two pointers that each move forward at most n times total are O(n), not O(n²), even though one loop sits inside the other.

The complexity classes, explained

O(1)
Constant

The same work regardless of input size. Constant does not mean fast — it means flat.

How hash tables reach O(1) →
O(log n)
Logarithmic

Throws away half the remaining work each step. A million items resolves in about 20 steps.

Binary search explained →
O(n)
Linear

Work grows in step with the input. One pass over an array or a linked list.

Array vs linked list →
O(n log n)
Linearithmic

The practical floor for comparison sorting. Merge sort and heap sort live here.

Bubble sort vs merge sort →
O(n²)
Quadratic

Every element compared against every other. Fine at n = 100, painful at n = 100,000.

Big O from scratch →
O(2ⁿ)
Exponential

Branching recursion that recomputes the same subproblems. The signal to reach for memoisation.

Dynamic programming →

Frequently asked questions

What is Big O notation?
Big O notation describes how the work an algorithm does grows as its input grows. It ignores constants and hardware and keeps only the dominant term, so 3n + 5 steps is written O(n). It is a statement about the shape of the growth curve, not about how many milliseconds something takes.
Does O(1) mean instant?
No. O(1) means constant, not fast. An O(1) operation takes the same time no matter how large the input is, but that constant could still be slow. An O(1) hash lookup that computes an expensive hash can lose to an O(n) scan over ten items.
Why do you drop constants and lower order terms?
Because Big O describes behaviour as the input grows towards infinity, where the largest term dominates everything else. At n = 1,000,000 the n² term in n² + 500n is about two thousand times larger than the rest, so the smaller terms stop mattering.
What is the difference between time and space complexity?
Time complexity counts how the number of operations grows with the input. Space complexity counts how much extra memory grows with it. They often trade against each other: a hash set can turn an O(n²) scan into O(n) time at the cost of O(n) extra space. Full comparison →
What is the difference between Big O, Big Theta and Big Omega?
Big O is an upper bound, the worst case. Big Omega is a lower bound, the best case. Big Theta applies when the two match, giving a tight bound. Interviewers usually say Big O but mean the tight worst case bound.
How do I work out the complexity of a loop?
Count how many times the loop runs relative to the input size, not the work inside it. A loop over every element is O(n). Nested loops multiply, so two nested loops over the same input are O(n²). Loops that halve the range each step are O(log n). Sequential loops add, and you keep the largest term.

Keep going