Big-O Cheat Sheet#

Big-O notation describes how an algorithm’s running time or memory grows as the input size n grows. It captures the worst-case trend, not the exact number of steps, so you can compare approaches without benchmarking. This page is a quick reference you can bookmark; each row links back to where the structure or algorithm shows up in the 60-day challenge.

The complexity classes, fastest to slowest#

NotationNameRough meaningExample
O(1)ConstantSame work regardless of nArray index lookup
O(log n)LogarithmicHalves the problem each stepBinary search
O(n)LinearWork grows with the inputScanning an array
O(n log n)LinearithmicSort-like divide and combineMerge sort
O(n²)QuadraticNested loops over the inputBubble sort
O(n³)CubicTriple nested loopsNaive all-pairs paths
O(2ⁿ)ExponentialDoubles per added elementNaive subset generation
O(n!)FactorialAll permutationsBrute-force travelling salesman

Rule of thumb for interviews: aim for O(n log n) or better on array/string problems, and treat O(2ⁿ)/O(n!) as a signal you need memoization, pruning, or a smarter pattern.

Data structure operations#

Average-case complexities (worst case in parentheses where it differs).

Data structureAccessSearchInsertDeleteSpace
ArrayO(1)O(n)O(n)O(n)O(n)
Dynamic arrayO(1)O(n)O(1)*O(n)O(n)
Singly linked listO(n)O(n)O(1)O(1)O(n)
StackO(n)O(n)O(1)O(1)O(n)
QueueO(n)O(n)O(1)O(1)O(n)
Hash tableO(1) (O(n))O(1) (O(n))O(1) (O(n))O(n)
Binary search treeO(log n) (O(n))O(log n) (O(n))O(log n) (O(n))O(log n) (O(n))O(n)
Balanced BST (AVL/Red-Black)O(log n)O(log n)O(log n)O(log n)O(n)
Binary heapO(1) peekO(n)O(log n)O(log n)O(n)

*Amortized: a dynamic array’s occasional resize is O(n), but averaged over many appends each push is O(1).

See these in the challenge: arrays, linked lists (Day 9), hash tables (Day 23), BSTs (Day 15), and heaps (Day 17).

Sorting algorithms#

AlgorithmBestAverageWorstSpaceStable
Bubble sortO(n)O(n²)O(n²)O(1)Yes
Selection sortO(n²)O(n²)O(n²)O(1)No
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
Quick sortO(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

Watch these run step by step in the sorting visualizer.

Graph algorithms#

For a graph with V vertices and E edges.

AlgorithmTimeSpace
Breadth-first search (BFS)O(V + E)O(V)
Depth-first search (DFS)O(V + E)O(V)
Dijkstra (binary heap)O((V + E) log V)O(V)
Bellman-FordO(V · E)O(V)
Floyd-WarshallO(V³)O(V²)
Topological sortO(V + E)O(V)

Interview pattern complexities#

PatternTypical timeWhen you reach for it
Two pointersO(n)Sorted arrays, pair/triplet sums
Sliding windowO(n)Contiguous subarray/substring problems
Binary searchO(log n)Sorted input or monotonic answer space
Hashing / frequency mapO(n)Lookups, dedupe, counting
BFS/DFS traversalO(V + E)Trees and graphs
Dynamic programmingO(n·states)Overlapping subproblems
BacktrackingO(bᵈ)Enumerate combinations/permutations

How to reason about Big-O quickly#

  1. Count nested loops over the input. One loop is O(n), a loop inside a loop is usually O(n²).

  2. Halving is a logarithm. Each time you cut the problem in half, add a log n factor.

  3. Drop constants and lower-order terms. O(2n + 5) is O(n); O(n² + n) is O(n²).

  4. Recursion → recurrence. Merge sort does two half-size calls plus O(n) work per level. Writing that as a recurrence and solving it:

    \(T(n) = 2\,T\!\left(\tfrac{n}{2}\right) + O(n) = O(n \log n)\)
  5. Space counts too. Recursion uses stack space; an O(n) auxiliary array is O(n) space even if the time is better.

Ready to put this into practice? Start the free 60-day challenge or jump to interview prep.