Big-O Notation Explained: Time Complexity Cheat Sheet with Examples [Interactive]

INSIGHTS6 min read
Big-O notation explained: time complexity cheat sheet

Big-O notation describes how the running time (or memory use) of an algorithm grows as the input gets bigger. It ignores constants and hardware and keeps only the part that dominates for large inputs. For example, checking every item in a list is O(n): twice the items, twice the work. Binary search on a sorted list is O(log n): a million items need only about 20 steps.

This guide explains the common complexity classes with code examples, shows how to calculate Big-O yourself, and lists the complexity of the data structures you use every day in Python, Java, C++ and SQL. Start with the interactive lab to see the difference with your own eyes.

Interactive Big-O lab

In Lab 1, change the number of items and watch how many steps each complexity needs. In Lab 2, step through a binary search and compare it with a linear search.

Lab 1 ยท Growth visualizer
Watch what happens when your data grows
Move the slider to change n, the number of items. The bars show how many steps each complexity needs, on a log scale. Times assume roughly 100 million simple steps per second.
n = 1,000 items
Lab 2 ยท Binary search, step by step
Find a number in a sorted list: linear vs binary search
Pick a target and press Next step. Binary search looks at the middle item and throws away the half that cannot contain the target. Linear search checks items one by one.

The common Big-O complexity classes

Big-ONameSteps for n = 1,000,000Typical example
O(1)Constant1Array index, hash table lookup
O(log n)Logarithmicabout 20Binary search, balanced tree lookup
O(n)Linear1,000,000Loop over a list, linear search
O(n log n)Linearithmicabout 20,000,000Merge sort, Timsort, efficient sorting
O(nยฒ)Quadratic1,000,000,000,000Nested loops, bubble sort
O(2โฟ)ExponentialimpossibleTrying every subset

The jump between classes matters far more than any constant factor. An O(nยฒ) algorithm on fast hardware still loses to an O(n log n) algorithm on slow hardware once the input is large enough.

Big-O with code examples

# O(1): one step, no matter how big the list is
first = items[0]

# O(n): one pass over the list
total = 0
for x in items:
    total += x

# O(nยฒ): a loop inside a loop over the same list
for a in items:
    for b in items:
        if a + b == target:
            print(a, b)

# O(log n): the problem halves every step
lo, hi = 0, len(items) - 1
while lo <= hi:
    mid = (lo + hi) // 2
    if items[mid] == target:
        break
    elif items[mid] < target:
        lo = mid + 1
    else:
        hi = mid - 1

How to calculate Big-O

  1. Count the loops. One loop over n items is O(n). A loop inside a loop over the same data is O(nยฒ).
  2. Steps in sequence add up, nested steps multiply. Two separate loops are O(n + n) = O(n). Nested loops over two different inputs are O(n ร— m), not O(nยฒ).
  3. Drop constants. O(2n) and O(n + 100) are both O(n).
  4. Keep only the dominant term. O(nยฒ + n) is O(nยฒ), because nยฒ dominates for large n.
  5. Halving means log n. If every step cuts the remaining work in half, the loop runs about logโ‚‚ n times.
  6. Watch for hidden loops. x in my_list, list.contains() and std::find are loops too.

Data structure complexity cheat sheet

StructureAccessSearchInsertDelete
Array / dynamic arrayO(1)O(n)O(1) amortized at the end, O(n) in the middleO(n)
Linked listO(n)O(n)O(1) at the headO(1) with a reference to the node
Hash table-O(1) average, O(n) worstO(1) averageO(1) average
Balanced binary search treeO(log n)O(log n)O(log n)O(log n)
Binary heapO(1) for min or maxO(n)O(log n)O(log n) for min or max
Stack / queueO(1) for the top or frontO(n)O(1)O(1)

What it looks like in your language

LanguageO(1) average lookupO(log n), sortedO(n) lookup (careful)
Pythondict, setbisect on a sorted listx in list, list.index()
JavaHashMap, HashSetTreeMap, TreeSetArrayList.contains()
C++std::unordered_mapstd::map, std::setstd::find on a vector
SQLHash index (some databases)B-tree index lookupFull table scan without an index

This is why a single index can turn a slow query into a fast one: without an index the database scans every row (O(n)), with a B-tree index it walks down a tree (O(log n)).

Sorting algorithm complexity

AlgorithmBestAverageWorstExtra spaceStable
Bubble sortO(n)O(nยฒ)O(nยฒ)O(1)Yes
Insertion sortO(n)O(nยฒ)O(nยฒ)O(1)Yes
Merge sortO(n log n)O(n log n)O(n log n)O(n)Yes
QuicksortO(n log n)O(n log n)O(nยฒ)O(log n)No
Heap sortO(n log n)O(n log n)O(n log n)O(1)No
Timsort (Python, Java objects)O(n)O(n log n)O(n log n)O(n)Yes

Big-O is not the whole story

Two O(n) programs can still differ ten times in speed. Big-O hides constants, and real hardware adds its own: walking through an array is fast because the items sit next to each other in memory and the CPU cache loads them in blocks, while a linked list jumps around memory. Knowing how the CPU, memory and caches work is what turns "the right complexity" into genuinely fast code.

Frequently asked questions

What is Big-O notation in simple terms?

Big-O notation describes how the work an algorithm does grows as its input grows. O(n) means the work grows in proportion to the input, O(nยฒ) means doubling the input quadruples the work, and O(1) means the work stays the same regardless of input size.

What is the difference between O(n) and O(log n)?

O(n) work grows in step with the input: a million items need about a million steps. O(log n) work grows very slowly because each step halves the problem: a million items need only about 20 steps, which is why binary search is so fast on sorted data.

How do you calculate the Big-O of a function?

Count how many times the main operations run as a function of the input size. Add the cost of steps in sequence, multiply the cost of nested loops, drop constant factors and keep only the fastest-growing term. Remember that built-in operations such as searching a list are loops too.

Is O(1) always faster than O(n)?

Not for small inputs. O(1) means the cost does not grow, but that constant cost can be large, such as computing a hash. For a handful of items a simple O(n) loop can be faster. For large inputs, the lower complexity always wins.

What is the time complexity of a hash table lookup?

On average a hash table lookup, insert or delete is O(1). In the worst case, when many keys collide, it can degrade to O(n). Modern implementations such as Java HashMap reduce the worst case by turning crowded buckets into balanced trees.

Why does Big-O matter in coding interviews?

Interviewers use Big-O to see whether you can reason about how a solution scales. Being able to state the time and space complexity of your code, and to improve it, for example from O(nยฒ) to O(n) with a hash set, is often as important as getting the right answer.

Where to go next

Big-O tells you how work grows. To understand why some code is still faster than other code with the same complexity, you need to know how the computer executes it: how the CPU, memory and input and output communicate, how numbers are stored and added in binary, and how logic gates build a processor. That is covered lesson by lesson in Low-Layer Computer Understanding and Hardware Foundations.