Skip to the workspace
Pebellslearn by building
Foundations: Complexity & Arrays  ›  Lesson

Big-O & Complexity

Big-O Notation 22 minPredicting how work grows as the data grows

dsa / Big-O & Complexity

Explore each scene at your own pace.

01 · Search Tunebox's library

Search Tunebox's library

Big-O & Complexity

Definition
Big-O notation describes how the number of basic steps an operation takes grows as the size of its input, n, grows, keeping only the dominant term, such as O(1), O(log n), O(n) or O(n²).
In plain words
Instead of timing code with a stopwatch, you ask: if the input doubles, does the work stay the same, double, or quadruple? Big-O is the name for that answer, and it tells you which approach will still feel fast with a million items.

In short: a data structure is just an organised way to hold data — an array, a hash table, a tree — chosen so the operations you actually need stay cheap as the data grows. Big-O is how you compare them: not a stopwatch reading, but how many basic steps an operation takes for an input of size n, and how that count grows as n grows. Pick the wrong structure and a feature that felt instant at 40 songs crawls at 40 million. This whole course is "know the shape of the cost before your users find it."

A race track with six runners labelled O(1) through O(2^n): the O(1) and O(log n) runners barely move as the track gets longer, O(n) keeps pace with it, and O(n²) and O(2^n) are already off the edge of the paper

Search Tunebox's library

A scan checks every song until it finds a match:

python

Same code, wildly different cost depending only on where the song sits:

watch it move · array

The same scan costs 1 step or n steps depending only on where the song sits, which is why a bare Big-O quotes the worst case.

Looking for Glow: it sits in slot 0, so the scan stops after 1 comparison. That's the best case. 5 cells. At index 0. 1 of 5 cells settled.

1 / 6

That's best case (1 step), worst case (n steps) and average case (about n/2 steps) — one function, three numbers. A bare "scanning is O(n)" means the worst case — the promise you make to every user. The mistake: test with the lucky song at position 0 and promise that speed to everyone.

Binary search beats a scan by halving the field each step instead of walking it one at a time — but only works on a sorted list:

step through it
1n = 20
2steps = 0
3while n > 0:
4 n = n // 2
5 steps += 1
6print(steps)

Doubling n adds only one more halving: 1,000 songs takes 10 steps, a million takes 20. A dict lookup by id skips the search entirely — O(1), one hash, done. Three structures, three costs for "find one song":

Big-O name Tunebox example
O(1) constant look up a song by id in a dict
O(log n) logarithmic binary search a sorted list of titles
O(n) linear scan every song for a title
O(n log n) linearithmic sort the library by play count
O(n²) quadratic compare every pair of songs for duplicates
O(2ⁿ) exponential try every subset of songs to fill exactly 60 minutes

Big-O also drops constants and small terms — 2n and 500n are both O(n), and 3n² + 5n + 100 is O(n²), because at n = 1,000 the n² term is 3,000,000 and the rest is a rounding error. Loops one after another add (two n-loops back to back is still O(n)); loops nested inside each other multiply (O(n²)):

What does this print?
def work(n):
    steps = 0
    for i in range(n):
        steps += 1
    for i in range(n):
        steps += 1
    return steps

print(work(10), work(20))
Scene 1 of 6
Project context and further resources
You're building a piece ofTunebox — the library
This piece — steps_to_find(): Predicts which lookups stay fast as the library grows.
Scenario Tunebox will grow from a few hundred songs to millions. The scale planner predicts which lookups stay instant as the library grows — and which grind to a halt.
steps_to_find.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
steps_to_find(n, method) → intWorst-case steps a search method takes to find one song among n.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.