Quick facts for the interview. Skim the last section before you walk in.
| Class |
Name |
Typical example |
| O(1) |
Constant |
Hash map lookup, array index |
| O(log n) |
Logarithmic |
Binary search |
| O(n) |
Linear |
One pass over an array |
| O(n log n) |
Linearithmic |
Merge sort, heap sort, sorted() |
| O(n²) |
Quadratic |
Two nested loops over the input |
| O(2^n) |
Exponential |
Generating all subsets |
| O(n!) |
Factorial |
Generating all permutations |
| Constraint on n |
Complexity you need |
Why |
| n ≤ 10 |
O(n!) is fine |
10! is about 3.6 million |
| n ≤ 20 |
O(2^n) is fine |
2^20 is about 10^6 |
| n ≤ 10^3 |
O(n²) is fine |
About 10^6 operations |
| n ≤ 10^5 |
O(n log n) or better |
O(n²) is about 10^10, too slow |
| n ≤ 10^6 |
O(n) or O(n log n) |
O(n²) is far too slow |
| n ≤ 10^9 |
O(log n) or O(1) |
Even O(n) is too slow |
Format is average / worst where they differ. A single value means both are the same.
An asterisk means amortized.
| Structure |
Access |
Search |
Insert |
Delete |
| Array (fixed) |
O(1) |
O(n) |
O(n) |
O(n) |
| Dynamic array (Python list, ArrayList, JS array) |
O(1) |
O(n) |
O(1)* end, O(n) middle |
O(1) end, O(n) middle |
| Hash map / set |
n/a (by key) |
O(1) / O(n) |
O(1) / O(n) |
O(1) / O(n) |
| Stack (array-backed) |
top O(1) |
O(n) |
push O(1)* |
pop O(1)* |
| Queue (deque) |
front O(1) |
O(n) |
enqueue O(1) |
dequeue O(1) |
| Singly linked list |
O(n) |
O(n) |
O(1) at head |
O(1) at head, O(n) by node |
| Binary heap |
peek O(1) |
O(n) |
push O(log n) |
pop O(log n) |
| Balanced BST |
min / max O(log n) |
O(log n) |
O(log n) |
O(log n) |
| Trie (L = word length) |
n/a |
O(L) |
O(L) |
O(L) |
Building a heap from n items with heapify is O(n), not O(n log n).
| Algorithm |
Best |
Average |
Worst |
Space |
Stable |
| Quicksort |
O(n log n) |
O(n log n) |
O(n²) |
O(log n) |
No |
| Merge sort |
O(n log n) |
O(n log n) |
O(n log n) |
O(n) |
Yes |
| Heap sort |
O(n log n) |
O(n log n) |
O(n log n) |
O(1) |
No |
| Counting sort |
O(n + k) |
O(n + k) |
O(n + k) |
O(n + k) |
Yes |
| Bucket sort |
O(n + k) |
O(n + k) |
O(n²) |
O(n + k) |
Depends on inner sort |
For counting and bucket sort, k is the range of values or the number of buckets.
Quicksort’s worst-case space is O(n).
Built-in sorts:
| Built-in |
Algorithm |
Time |
Space |
Stable |
Python sorted() / list.sort() |
Timsort |
O(n) best, O(n log n) worst |
O(n) |
Yes |
Java Arrays.sort (objects), Collections.sort |
Timsort |
O(n) best, O(n log n) worst |
O(n) |
Yes |
JS Array.prototype.sort (V8) |
Timsort |
O(n log n) typical |
varies |
Yes (ES2019+) |
Java primitive arrays use dual-pivot quicksort, which is not stable. Stability does not matter for plain numbers.
| Algorithm |
Time |
Space |
Notes |
| BFS |
O(V + E) |
O(V) |
Queue. Shortest path in an unweighted graph |
| DFS |
O(V + E) |
O(V) |
Recursion or explicit stack |
| Topological sort |
O(V + E) |
O(V) |
Kahn’s algorithm (queue) or DFS |
| Dijkstra (binary heap) |
O((V + E) log V) |
O(V + E) |
Non-negative weights only |
| Binary search |
O(log n) |
O(1) iterative |
O(log n) space if recursive |
| Tree traversal (DFS) |
O(n) |
O(h) |
h is height: O(log n) balanced, O(n) skewed |
| Operation |
Cost |
Do this instead |
list.pop(0) |
O(n) |
collections.deque with popleft(), O(1) |
list.insert(0, x) |
O(n) |
deque.appendleft(x), O(1) |
x in list |
O(n) |
x in set, average O(1) |
list.remove(x), list.index(x) |
O(n) |
Keep a dict or set for lookups |
a[i:j] slicing |
O(j - i), copies |
Use indices if you don’t need a copy |
s += x in a loop |
Can be O(n²) |
Collect parts in a list, then "".join(parts) |
heapq |
Min-heap only |
Push -x to simulate a max-heap |
sorted(a) |
O(n log n) |
Expected. Don’t write your own |
| Operation |
Cost |
Do this instead |
arr.shift() |
O(n) |
Keep a head index instead |
arr.unshift(x) |
O(n) |
Keep a head index, or use a deque |
arr.includes(x), arr.indexOf(x) |
O(n) |
Set.has(x), average O(1) |
arr.splice(i, 1) |
O(n) |
Avoid inside loops |
arr.slice(i, j) |
O(j - i), copies |
Use indices if you don’t need a copy |
| No built-in heap |
n/a |
Write a binary heap |
arr.sort() |
Compares as strings: [10, 9, 1] becomes [1, 10, 9] |
arr.sort((a, b) => a - b) |
s += x in a loop |
Engines often optimize, not guaranteed |
Collect parts, then parts.join('') |
| Operation |
Cost |
Do this instead |
s += x in a loop |
O(n²) total |
StringBuilder.append(x) |
ArrayList.remove(0) |
O(n) |
ArrayDeque.pollFirst(), O(1) |
ArrayList.add(0, x) |
O(n) |
ArrayDeque.addFirst(x), O(1) |
LinkedList.get(i) |
O(n) |
ArrayList with get(i), O(1) |
PriorityQueue.contains, remove(Object) |
O(n) |
Lazy deletion, or avoid |
PriorityQueue |
Min-heap: peek O(1), poll and offer O(log n) |
Comparator.reverseOrder() for max |
HashMap get / put |
Average O(1) |
Default choice |
TreeMap get / put |
O(log n) |
Only when you need sorted keys |
- Always state time and space together, e.g. “O(n) time, O(n) space for the hash map.”
- Define n in the sentence: “n is the length of
nums.” With two inputs, name both.
- Count recursion. Each call uses stack space, so a recursive DFS on a skewed tree is O(n) space.
- Say amortized vs worst case: “append is O(1) amortized, but one resize is O(n).”
- Hash map operations are average-case O(1). The worst case is O(n) with many collisions.
- A sort step is O(n log n). It sets the bound unless the rest of the solution is slower.