Summary & Final Review
One map of the whole course, a cheat sheet, and a final mixed test.
- see the whole course as a small set of reusable ideas
- pick an algorithm or data structure from a problem statement
- recall the main complexities
- check yourself with a mixed final test
The whole course on one page
Twelve weeks, but only a handful of big ideas. Use this page as a map when you revise.
| Idea | Where you saw it | Core cost |
|---|---|---|
| Count operations, ignore constants | Week 1–2 | O, Ω, Θ |
| Halve the problem | binary search, merge sort, fast power, quickselect | log n / n log n |
| Choose a clever data structure | stack, queue, hash table, heap, BST, AVL, trie, union–find | O(1) – O(log n) per operation |
| Explore a graph | BFS, DFS, topological sort | O(n + m) |
| Be greedy (with proof) | Dijkstra, Kruskal, Prim, interval scheduling, Huffman | usually O(m log n) or O(n log n) |
| Remember subproblems | Fibonacci, grid paths, LCS, edit distance, matrix chain, knapsack | #subproblems × work each |
| Reduce one problem to another | matching → flow, LP, NP-completeness | polynomial vs presumed exponential |
Complexity cheat sheet
Sorting and searching
| Algorithm | Time | Notes |
|---|---|---|
| Linear search | O(n) | any list |
| Binary search | O(log n) | sorted list |
| Selection / insertion / bubble sort | O(n²) | insertion: O(n) if already sorted |
| Merge sort | Θ(n log n) | O(n) extra space, stable |
| Quicksort | O(n log n) average, O(n²) worst | in place |
| Heap sort | Θ(n log n) | in place, not stable |
| Quickselect | O(n) expected | median of medians: O(n) worst |
Data structures
| Structure | Search | Insert | Delete | Notes |
|---|---|---|---|---|
| Array / list | O(n) | O(n) middle, O(1) end | O(n) | O(1) index |
| Linked list | O(n) | O(1) at known place | O(1) at known place | |
| Hash table | O(1) avg | O(1) avg | O(1) avg | worst O(n) |
| Binary heap | O(n) | O(log n) | O(log n) max/min | peek max O(1) |
| BST | O(h) | O(h) | O(h) | h up to n |
| AVL tree | O(log n) | O(log n) | O(log n) | always balanced |
| Trie | O(L) | O(L) | O(L) | L = word length |
| Union–Find | find ≈ O(1) | union ≈ O(1) | — | amortised α(n) |
Graph algorithms (n vertices, m edges)
| Algorithm | Problem | Time |
|---|---|---|
| BFS | reachability, fewest edges | O(n + m) |
| DFS | components, cycles, ordering | O(n + m) |
| Topological sort | order a DAG | O(n + m) |
| Dijkstra (heap) | shortest paths, weights ≥ 0 | O((n + m) log n) |
| Bellman–Ford | shortest paths, negative edges | O(nm) |
| Floyd–Warshall | all-pairs shortest paths | O(n³) |
| Kruskal | MST | O(m log m) |
| Prim (heap) | MST | O(m log n) |
| Edmonds–Karp | maximum flow | O(n·m²) |
Strings and DP tables
| Algorithm | Time |
|---|---|
| Brute-force matching | O(nm) |
| KMP | O(n + m) |
| Boyer–Moore | sub-linear typical |
| LCS / edit distance | O(mn) |
| Matrix chain | O(n³) |
| 0/1 knapsack | O(nW) |
Which technique? A decision guide
| The problem says… | Try… |
|---|---|
| sorted data, find an item or position | binary search |
| 'fewest steps / moves / edges' | BFS |
| 'is it reachable', 'how many groups', 'find a cycle' | DFS / BFS / union–find |
| tasks with prerequisites | topological sort (DAG); longest path = critical path |
| cheapest route with positive weights | Dijkstra |
| cheapest way to connect everything | MST (Kruskal / Prim) |
| always need the biggest / smallest next | heap (priority queue) |
| count distinct / 'have I seen it?' | hash set / dict |
| maximum number of non-overlapping intervals | greedy: sort by finish time |
| best over choices, overlapping subproblems, greedy fails | dynamic programming |
| maximise throughput through a capacity network, matching | max flow |
| find a pattern in a long text | KMP / Boyer–Moore |
| every known algorithm is exponential, looks like SAT / TSP / subset sum | NP-hardness: approximate or restrict |
Before coding, estimate the cost: with n = 10⁵, an O(n²) plan is 10¹⁰ steps — too slow in Python. Ask whether sorting, a heap, a hash table or a different technique brings it to O(n log n).
Common mistakes to avoid
- Running binary search on unsorted data.
- Using
list.pop(0)as a queue (O(n)) instead ofdeque.popleft(). - Applying Dijkstra with negative edge weights.
- Using greedy without a correctness argument (coin change!).
- Writing
[[0]*c]*rfor a matrix — all rows are the same list. - Making two recursive calls where one would do (fast power).
- Forgetting that a hash table's worst case is O(n), and that dictionary keys must be immutable.
- Forgetting that BST height depends on insertion order.
Final test — mixed questions from all weeks
T(n) = 2T(n/2) + n. The solution is…
Selection sort stopped after two rounds. The list is…
L = [5, 2, 9, 1, 7]
for i in range(len(L) - 1):
m = i
for j in range(i + 1, len(L)):
if L[j] < L[m]:
m = j
L[i], L[m] = L[m], L[i]
if i == 1:
break
print(L)Which structure has O(1) average-time membership test?
What topological order does this print?
adj = {1: [2, 3], 2: [4], 3: [4], 4: [5], 5: []}
indeg = {v: 0 for v in adj}
for u in adj:
for v in adj[u]:
indeg[v] += 1
ready = [v for v in adj if indeg[v] == 0]
order = []
while ready:
u = ready.pop(0)
order.append(u)
for v in adj[u]:
indeg[v] -= 1
if indeg[v] == 0:
ready.append(v)
print(order)Which algorithm solves single-source shortest paths when edges can be negative?
A min-heap holds 1,023 items. Roughly how many swaps may one insertion need at most?
Match the algorithm to its first step — order Dijkstra's algorithm.
Which problems are solved by a greedy algorithm with a correctness proof?
Edit distance between 'cat' and 'cut'?
What does this print? (type the exact output)
def f(n):
if n <= 1:
return 1
return f(n - 1) + f(n - 2)
print(f(6))A text has length 10⁶ and a pattern has length 100. Which gives a worst-case guarantee of about 10⁶ steps?
You need the 3 largest of 10⁶ numbers. A good approach is…
Which is not known to be in P?
What does this print? (type the exact output)
from collections import deque
def bfs_dist(adj, s, t):
d, q = {s: 0}, deque([s])
while q:
u = q.popleft()
for v in adj[u]:
if v not in d:
d[v] = d[u] + 1
q.append(v)
return d[t]
adj = {0: [1, 2], 1: [3], 2: [3, 4], 3: [5], 4: [5], 5: []}
print(bfs_dist(adj, 0, 5))Which of these guarantees O(log n) for every operation on n keys?
Practice on LeetCode
A mixed set that touches every part of the course — try to name the technique before you code.
Try each problem first with a brute-force idea and estimate its cost, then improve it using this chapter's technique. Links open LeetCode in a new tab; all problems listed are free. Levels follow LeetCode's own labels.
| Problem | Level | Practises | |
|---|---|---|---|
| Two Sum ↗ | Easy | hash table | |
| Valid Parentheses ↗ | Easy | stack | |
| Merge Intervals ↗ | Medium | sort + sweep | |
| Number of Islands ↗ | Medium | DFS / BFS / union–find | |
| Course Schedule ↗ | Medium | topological sort | |
| Network Delay Time ↗ | Medium | Dijkstra | |
| Kth Largest Element in an Array ↗ | Medium | heap or quickselect | |
| Coin Change ↗ | Medium | dynamic programming | |
| Longest Common Subsequence ↗ | Medium | dynamic programming on two strings | |
| LRU Cache ↗ | Medium | hash table + linked list design | |
| Implement Trie (Prefix Tree) ↗ | Medium | trie | |
| Min Cost to Connect All Points ↗ | Medium | MST |
Beyond LeetCode
- NeetCode practice list ↗ — a topic-organised set of LeetCode problems, if you want more volume
Key takeaways
- Always estimate cost first: counting operations and spotting n, n log n, n² or exponential growth tells you whether an approach will work.
- Recognise the pattern: halving → log; sort first; graph exploration → BFS/DFS; greedy needs proof; overlapping subproblems → DP; capacities → flow.
- Data structures are tools: pick one so the operation you repeat most often is cheap.
- Some problems (NP-complete) have no known fast algorithm — recognise them, then approximate or restrict.