Octonote AI
Make your own →

Note

Big-O for everyday data structures

Sunil Tiwari· Oct 12, 2026 · 6 min

Big-O notation describes how the cost of an operation grows as the input grows. It ignores constant factors and small inputs, and keeps only the shape of the curve. A hash map lookup is O(1)O(1) on average, so users.get(id) costs about the same with ten users or ten million. Scanning a list is O(n)O(n), so doubling the list doubles the work.

You rarely need proofs in day-to-day work. You need a feel for which operations are cheap on which structures, and for how fast the bad cases get bad.

The growth ladder

The classes you meet most often, with what they mean when nn is one million:

The growth ladder at one million items
The growth ladder at one million items
ClassNameSteps at n=106n = 10^6Everyday example
O(1)O(1)constant1a[i], map.get(k)
O(log⁡n)O(\log n)logarithmic~20binary search, heap push
O(n)O(n)linear10610^6one pass over a list
O(nlog⁡n)O(n \log n)linearithmic~2×1072 \times 10^7sorting
O(n2)O(n^2)quadratic101210^{12}comparing every pair
O(2n)O(2^n)exponentialmore than atoms in the universetrying every subset

At around 10810^8 simple steps per second, nlog⁡nn \log n finishes in a fraction of a second. n2n^2 takes hours. That gap, not the exact constant, is what Big-O is for.

Arrays and dynamic arrays

A contiguous block of memory. Python's list, JavaScript's Array, Java's ArrayList, Go slices and C++ std::vector all work this way.

OperationCostWhy
Read or write a[i]O(1)O(1)address = start + i × size
Append at the endamortised O(1)O(1)see below
Insert or delete at the front or middleO(n)O(n)everything after it shifts
Search, unsortedO(n)O(n)check each item
Search, sorted (binary search)O(log⁡n)O(\log n)halve the range each step

Why appending is amortised O(1)O(1)

When the array runs out of room, it allocates a block twice as large and copies everything across. That single append costs O(n)O(n). But it happens only at sizes 1, 2, 4, 8 and so on, so the total copying over nn appends is

1+2+4+⋯+n<2n1 + 2 + 4 + \dots + n < 2n

Spread over nn appends, that is a constant amount of extra work each. "Amortised" means exactly this: an occasional expensive step, paid for by many cheap ones.

Why appending is amortised O(1)
Why appending is amortised O(1)

Linked lists

Each node points to the next. Good at one thing: inserting or removing at a node you already hold.

OperationCost
Insert or delete at the headO(1)O(1)
Insert or delete after a node you holdO(1)O(1)
Access the ii-th itemO(n)O(n)
SearchO(n)O(n)

In practice arrays often beat linked lists even where the table says otherwise, because walking scattered nodes misses the CPU cache on almost every step.

Stacks, queues and deques

  • Stack (last in, first out): push and pop at one end, O(1)O(1). A dynamic array does this well.
  • Queue (first in, first out): enqueue at the back, dequeue at the front, O(1)O(1). Use a ring buffer or a deque, not list.pop(0) in Python, which is O(n)O(n).
  • Deque: push and pop at both ends in O(1)O(1). Python's collections.deque is the usual answer.

Hash maps and hash sets

A hash function turns the key into a bucket number, so the map jumps straight to the right place instead of searching.

Inside a hash map
Inside a hash map
OperationAverageWorst case
Get, put, deleteO(1)O(1)O(n)O(n)
Iterate all entriesO(n)O(n)O(n)O(n)
Find the smallest keyO(n)O(n)O(n)O(n)

The worst case appears when many keys land in the same bucket, either from a poor hash function or from an attacker who chooses colliding keys on purpose. Real implementations defend against it with randomised hashing, and Java switches long chains to balanced trees. When the table gets about three quarters full, it doubles and rehashes, which is amortised O(1)O(1) for the same reason array appends are.

Hash maps keep no order. If you need "the smallest key" or "all keys between 10 and 20", you want a tree.

Balanced binary search trees

Red-black trees, AVL trees and B-trees keep keys sorted and the tree height near log⁡n\log n. Java's TreeMap, C++ std::map and database indexes are built on them.

OperationCost
Search, insert, deleteO(log⁡n)O(\log n)
Min or maxO(log⁡n)O(\log n)
Range query returning kk itemsO(log⁡n+k)O(\log n + k)
In-order iterationO(n)O(n)

An unbalanced BST degrades to a linked list when keys arrive in sorted order, and every operation becomes O(n)O(n). Always use a self-balancing variant.

Heaps (priority queues)

A heap keeps the smallest (or largest) item on top without sorting the rest.

OperationCost
Peek at the minO(1)O(1)
PushO(log⁡n)O(\log n)
Pop the minO(log⁡n)O(\log n)
Build from nn itemsO(n)O(n)
Search for an arbitrary itemO(n)O(n)

Heaps power schedulers, "top kk" queries, and graph algorithms like Dijkstra's shortest path.

Choosing a structure

Start from the operation that has to be fast, then pick the structure that makes it cheap:

Pick a structure by the operation you need
Pick a structure by the operation you need

Sorting

AlgorithmBestAverageWorstExtra memoryStable
Merge sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n)O(n)yes
Heap sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(1)O(1)no
Quick sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n2)O(n^2)O(log⁡n)O(\log n)no
Timsort (Python, Java objects)O(n)O(n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n)O(n)yes
Insertion sortO(n)O(n)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)yes
Counting sortO(n+k)O(n + k)O(n+k)O(n + k)O(n+k)O(n + k)O(k)O(k)yes

Any sort that works by comparing items needs Ω(nlog⁡n)\Omega(n \log n) comparisons in the worst case. Counting sort and radix sort get around that by not comparing at all, which only works when keys are small integers (range kk) or fixed-width.

Insertion sort looks bad on paper but wins on tiny or nearly sorted inputs, which is why Timsort uses it for short runs.

Reading Big-O honestly

  • Constants matter at small nn. An O(n)O(n) scan of 50 items in a cache-friendly array can beat an O(1)O(1) hash lookup.
  • Average is not worst. Hash maps and quick sort have bad worst cases. Know when an attacker or unlucky input can trigger them.
  • Amortised is not every time. An append that triggers a resize really does pause. That matters in latency-sensitive code.
  • Watch hidden loops. x in some_list inside a loop is O(n2)O(n^2) overall. Switching to a set makes it O(n)O(n).
  • Memory has a cost too. Space complexity follows the same notation and can be the real constraint.
Was this useful?
Written bySunil Tiwari
Make a page like this. Free.Notes and diagrams side by side, written by you or by Claude Code and Codex. Publish it in one click.
Make your own, free