Exercises
Four lanes of book-scoped work. Each lane pulls from CLRS or a supporting text and targets a different mental muscle: proofs, implementation, pragmatics, contest-shaped problems. Aim for at least one lane per week during the module.
Lane 1 - CLRS Proof Drills
Target outcome: produce rigorous, short proofs of canonical balanced-tree and amortized-analysis properties.
Chunks:
- CLRS 13.1 Properties of red-black trees
- CLRS 13.2 Rotations
- CLRS 13.3 Insertion
- CLRS 16.1 Aggregate analysis
- CLRS 16.2 Accounting method
- CLRS 16.3 Potential method
- CLRS 16.4 Dynamic tables
- CLRS 19.4 Analysis of union by rank with path compression
Exercises:
- Prove that any red-black tree on
ninternal nodes has height at most2 log2(n+1). Use the black-height invariant directly. - Show that a left rotation preserves the BST property. Identify the three parent pointers that must update.
- Prove that the dynamic array with doubling has
O(1)amortizedappendby (a) aggregate method, (b) accounting method ($3 per append), and (c) potential method withPhi(T) = 2 * size - capacity. Reconcile the three answers. - Prove that without path compression but with union by rank, a sequence of
moperations onnelements costsO(m log n). Then state and justify theO(m alpha(n))bound once path compression is added. - Use the potential method to show the amortized cost of a binary counter increment (over
nincrements starting at 0) isO(1). State yourPhi.
Completion: hand-write each proof in under one page; self-grade for unstated assumptions.
Lane 2 - Implementation from Book
Target outcome: translate book pseudocode into working, tested code; discover where implementation diverges from theory.
Chunks:
- CLRS 13.3 Insertion (for red-black insert)
- CLRS 19.3 Disjoint-set forests (for union-find)
- Sedgewick balanced trees (for rotation primitives)
- Competitive Programming 2.4.3 Segment tree
- Competitive Programming 2.4.4 Binary indexed (Fenwick) tree
- Competitive Programming 2.4.2 Union-find
Exercises:
- Implement red-black insert from CLRS 13.3. Add an invariant-checker that runs after every call and fails loudly on a violation. Feed
10,000random insertions. - Implement the disjoint-set forest with both union by rank and path compression from CLRS 19.3. Add an instrumentation counter for the number of pointer writes in
find; plot againstlog* non a logarithmic x-axis. - Implement a segment tree with lazy propagation from Competitive Programming 2.4.3. Support range
sumand rangeadd. Cross-check against a brute-force array on10^4random sequences. - Implement a Fenwick tree from Competitive Programming 2.4.4. Add one: a 2-D variant supporting rectangle-sum query.
- Compare measured runtimes of red-black insert vs skip list insert on
n = 10^6keys; discuss where the constants come from.
Completion: each implementation runs, passes randomized cross-checks, and has a short (two-paragraph) lab notebook.
Lane 3 - Skiena Pragmatics
Target outcome: practice picking the right structure for a real engineering constraint.
Chunks:
Exercises:
- Your service processes 10,000 request/sec of high-cardinality string keys. Describe the memory footprint and throughput trade-off between a hash set, a red-black tree, a Bloom filter, and a trie. Pick one and justify.
- You must compute an MST on a graph with
V = 10^6vertices andE = 5 * 10^6weighted edges. Pick Kruskal with union-find vs Prim with a Fibonacci heap. Justify by asymptotic and constant-factor reasoning. - Design a "user saw this recently" cache for a social feed, budget 128 MB. You will accept some false positives. Size a Bloom filter; describe what happens at the 1-year mark when
nhas grown 10x. - Your analytics pipeline answers range-sum queries on a constantly mutating integer series of length
10^8. Select a data structure. Justify segment tree vs Fenwick vs prefix-sum array.
Completion: each answer fits in 300 words and references a concrete bound.
Lane 4 - Contest / Engineering Problem Set
Target outcome: apply the module's primary structures to an end-to-end problem under time pressure.
Chunks:
- Competitive Programming 4.3 Minimum spanning tree
- Competitive Programming 2.4.2 Union-find
- Competitive Programming 2.4.3 Segment tree
- Competitive Programming 2.4.4 Fenwick tree
Exercises:
- "Dynamic connectivity (incremental)." Input:
n <= 10^5nodes andq <= 10^5operations each of formunion(u, v)orconnected?(u, v). Implement with union-find + path compression. Time target: under 200 ms in your chosen language. - "Range sum with point update." Input: array
a[1..n]withn <= 10^6andq <= 10^6mixed operations. Implement a Fenwick tree. Time target: under 500 ms. - "Range add, range sum." Input: array
a[1..n]withn, q <= 10^5. Implement segment tree with lazy propagation. - "Offline MST on dense graphs." Input:
V <= 2000, complete weighted graph. Implement Kruskal with union-find. Verify weight against Prim-with-heap output. - "Deduplicate a 100 GB log stream on a 128 MB budget." Implement a Bloom filter-based dedup with measured false-positive rate and justified
m,k.
Completion: each problem solved, timed, and writeup includes the chosen asymptotic bound and measured wall-clock time.
Completion Checklist
- all four lanes have at least three exercises completed
- Lane 1 proofs are hand-verified for at least three items
- Lane 2 implementations run on randomized cross-checks without failures
- Lane 3 decisions cite concrete bounds, not hand-waves
- Lane 4 problems meet stated time budgets; at least one is benchmarked against a reference implementation
- notebook entries exist for each completed exercise (date, time-to-solve, main bug)