Guess the Big O — Data Structures

Guess the Big O: 15 levels reading code and picking O(1), O(n), O(log n) or O(n²). An algorithm complexity game for insight into code performance.

What you learn in this game

Big O measures how fast the number of steps grows as the input gets bigger. O(1) = constant (always 1 step). O(n) = grows linearly. This race shows the difference.

Nested loops produce O(n²). Every outer turn triggers n inner turns. For n = 100 → 10,000 steps. For n = 1000 → 1 million steps. It grows very fast.

O(log n) appears when the problem is halved every step. An array of 1 million items → only 20 steps of binary search.

Beware of "cheap" operations that are really linear: .includes(), .indexOf(), .find() are all O(n). Used inside a loop = O(n²). Set/Map = O(1) lookup.

O(2ⁿ) = exponential. Every call branches in two. fib(40) without a memo = hundreds of millions of calls. Memoization (storing results) cuts the recomputation → O(n). A key technique in dynamic programming.

Big O hides constants. O(2n) and O(n) are both written O(n). But in practice the constant still matters: 2n is slower than n. Combine loops when you can.

What the levels ask

  1. Who reaches the finish line first?
  2. Place your ticket first, then watch the race.
  3. Both search for the target. Which is more efficient?
  4. Bagas uses a Set, Bang Arka uses Array.includes. Which is faster?
  5. Two ways to find twin sacks. Which one wins?
  6. Two ways to compute fib(n). Place your ticket.
  7. Bagas loops twice. Bang Arka loops once. Which gets there first?
  8. Bagas uses recursion, Bang Arka uses a loop. The steps are the same, so who wins?
  9. Place your ticket, then watch.
  10. Bagas checks every number. Bang Arka stops at the square root.
  11. Find the pair that adds up to the target (sorted array).
  12. Find a two-sum in a shuffled array.

And more, 15 levels in all.