Data structures

TopicTimeSpace
Dynamic arrayIndex O(1); append O(1) amortized; insert or delete in the middle O(n)O(n)
Hash table (dict and set)Lookup, insert, delete O(1) expected; O(n) worst caseO(n)
StackPush, pop, peek O(1)O(n)
Queue and dequeEnqueue and dequeue O(1) with a dequeO(n)
Binary heap and priority queuePeek O(1); push and pop O(log n); build from n items O(n)O(n)
Binary search treeSearch, insert, delete O(h): O(log n) balanced, O(n) worst caseO(n)
Union-Find (disjoint-set union)Amortized O(α(n)) per find or unionO(n)
Monotonic stackO(n) totalO(n)

Sorting

TopicTimeSpace
MergesortΘ(n log n) in every caseO(n)

Searching

TopicTimeSpace
Binary searchO(log n)O(1) iterative

Graphs and trees

TopicTimeSpace
Breadth-first search (BFS)O(V + E)O(V)
Depth-first search (DFS)O(V + E)O(V)
Tree traversalsO(n)O(h) for depth-first orders
Dijkstra's algorithmO((V + E) log V) with a binary heapO(V + E)
Graph representationsEdge check: matrix O(1), list O(deg(u))List O(V + E); matrix O(V²)

Patterns

TopicTimeSpace
Two pointersO(n)O(1)
Sliding windowO(n)O(1) or O(n)
Prefix sumsO(n) to build; O(1) per range sumO(n)
Hash map frequency countingExpected O(n) totalO(n)
Backtracking and subsetsO(2ⁿ) subsets; O(n · 2ⁿ) including copying each oneO(n) recursion, plus stored output
Dynamic programmingNumber of states × transitions per stateDepends on memoization or tabulation
Merge intervalsO(n log n)O(n)

Rough guide: input size to complexity

Input size nComplexity that usually fits
Up to about 10O(n!)
Up to about 20O(2ⁿ)
Up to about 500O(n³)
Up to about 5,000O(n²)
Up to about 1,000,000O(n log n)
Up to about 100,000,000O(n)

Assumes roughly 10⁸ simple operations per second. Real limits depend on the language and the work per step.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list