Week 20 — Cache & High-Throughput Design
Month 05 · Week 20 · Theme: O(1) data structures behind every production system
This week is the design round that’s really an algorithms round. Every problem here is “build a textbook DS from scratch with O(1) guarantees” — exactly the trap question that separates a Mid-level fumble from a Staff-level reflex.
Why This Week Matters
LRU was Week 4 (p16). Every other cache, rate limiter, ranker, and suggestion engine you’ll ever build derives from the same three primitives this week drills:
- Doubly-linked list + hashmap → eviction in O(1).
- Sorted-by-frequency bucket of nodes → LFU in O(1) (not O(log n)).
- Heap-merge of k sorted streams → personalized timelines in O(k log k).
- Trie + per-node top-k heap → autocomplete in O(prefix + suggestions).
- Pre-bucketed time windows → rate limiting in O(1) amortized.
You will fail the design round if you reach for dict + sorted() on the eviction path. These five problems show you what a real production cache, feed, suggester, or rate limiter looks like — and what the interviewer wants to hear when they ask “now scale it 1000×”.
Daily Schedule
| Day | Problem | Pattern |
|---|---|---|
| Mon | p101 — LFU Cache | LC 460 · doubly-linked-list-of-lists, O(1) |
| Tue | p102 — Design Hit Counter | LC 362 · sliding-window time bucket |
| Wed | p103 — Design Twitter | LC 355 · k-way heap merge of user feeds |
| Thu | p104 — Design Autocomplete System | LC 642 · trie + per-prefix top-k heap |
| Fri | p105 — All O(1) Data Structure | LC 432 · bucket DLL of equal-count keys |
Readiness Gate
By Friday you should be able to:
- Implement LFU in 25 min with both
getandputstrictly O(1) — and explain why themin_freqpointer never needs to scan. - Implement a hit counter with both naive
dequeand bucketedcircular arrayvariants and explain when each wins (single-thread vs. wide-time-range queries). - Implement Twitter’s
getNewsFeedas a k-way heap merge in O(k log n) and articulate the alternative fan-out-on-write tradeoff. - Implement Autocomplete with trie + top-k heap maintained per node, including the input-stream state machine (
'#'resets) — most candidates forget the state. - Implement All O(1) inc/dec/getMax/getMin without a
SortedDict, using bucket DLLs — the canonical “I’ve actually built one of these” signal.
Connection to Interview Loop
These are the system-design-but-really-coding problems at Meta (LFU, Twitter), Google (Autocomplete), Microsoft (Hit Counter), and Bloomberg (All O(1)). Failure mode at Senior+ is always the same: candidate reaches for sorted() or heapify() in the hot path. The interviewer says nothing, marks the candidate down one level, and the loop is over.
The transferable lesson: every O(log n) call on a per-request path is a downlevel signal. If you internalize that this week, you’re playing at Staff caliber.