A free lesson. A small step forward.

Make sense of Big O

Learn to tell a single pass from a nested loop—and explain how their work grows.

Count the work, not the seconds

Imagine checking every name on a class list. With 10 names, you check 10 entries. With 100 names, you check 100. The amount of work grows with the list: this is linear growth.

Big O describes an upper bound on how an algorithm's work grows as its input grows. It does not tell you how many seconds your laptop will take.

One pass: O(n)

for name in names:
    print(name)

For a list of n names, the loop runs n times. Assuming each print takes a fixed amount of work, doubling the number of names doubles the work. We describe this as O(n).

Every pair: O(n²)

for first in names:
    for second in names:
        compare(first, second)

For each of n names, the inner loop runs n times. That means n × n comparisons, assuming a comparison takes fixed work. Double n and you get four times as many comparisons.

Number of operations in these two examples
NamesOne passEvery pair
1010100
2020400
10010010,000

Two loops do not always mean n²

Two separate passes take n + n = 2n operations. Their growth is still linear, O(n). Two nested loops may be quadratic, but you need to check how often each one actually runs.

A tighter description for our single-pass example is Θ(n). Big O is an upper bound; when comparing these examples, we use their usual tight growth classes.

Your turn.

Try from memory, then compare with the explanation. Your answers stay on this page and aren’t saved or graded.

A program scans a list of n names twice, in two separate loops. What is its growth class?

Reveal answer and explanation

O(n). There are 2n checks, so doubling the list doubles the work. The passes are separate, not nested.

The every-pair example makes 400 comparisons for 20 names. How many comparisons does it make for 40 names?

Reveal answer and explanation

1,600 comparisons. The two loops each run 40 times, so 40 × 40 = 1,600. Doubling the input quadruples the work.

Now make it your course.

Bring your own materials for a daily study path, personalized lessons, and feedback from Nova.

Start my study planExplore another topic