System Design DSA — Master Recap & Pattern Cheatsheet

Sanjeev SharmaSanjeev Sharma
4 min read

Advertisement

System Design DSA — Master Recap

Pattern → Problem Mapping

PatternProblemsCore Structures
DLL + HashMapLRU CacheO(1) get/put
FreqMap + DLL + HashMapLFU CacheO(1) freq-aware eviction
Global Timer + HeapTwitter Feedk-way merge
HashMap path stringsFile SystemO(L) create/get
Circular ArrayHit CounterO(1) time-bucketed
Binary Search on timestampsTime-Based KVO(log n) get
Trie + Freq MapAutocompleteO(L) prefix search
Array + Index PointerBrowser HistoryO(1) nav
Max-Heap + Min-HeapMedian FinderO(log n) add
Skip ListSorted StructureO(log n) expected

The Decision Framework

Need O(1) lookup by key?
HashMap as base

Need ordering (min/max/sorted)?
+ Heap, TreeMap, or Sorted List

Need O(1) move-to-front/back?
+ Doubly Linked List

Need frequency tracking?
Two HashMaps (key→freq, freq→OrderedSet)

Need prefix matching?
Trie

Need range queries on timestamps?
Sorted list + Binary Search

Need running statistics (median)?
Two Heaps (lo max-heap, hi min-heap)

LRU vs LFU Comparison

FeatureLRULFU
EvictionLeast recently usedLeast frequently used (+ LRU tie-break)
Structures1 DLL + 1 HashMap2 DLL + 2 HashMap + min_freq
ComplexityO(1)O(1)
Use caseWeb cache, CDNDatabase buffer pool

Two-Heap Pattern (Median)

lo = max-heap  (lower half, negate values)
hi = min-heap  (upper half)

# Invariant: len(lo) >= len(hi) and lo.max <= hi.min
def addNum(n):
    push to lo
    move lo.top to hi
    if len(hi) > len(lo): move hi.top to lo

def median():
    if odd: return lo.top
    else: return (lo.top + hi.top) / 2

Skip List Levels

Probability of reaching level k = (1/2)^k
Expected levels = O(log n)
Expected search cost = O(log n)

_random_level():
    level = 1
    while random() < 0.5 and level < MAX:
        level += 1
    return level

Circular Buffer Formula

tail = (tail + 1) % capacity   # advance tail
head = (head + 1) % capacity   # advance head
full  → size == capacity
empty → size == 0

Quick Reference: All 20 Problems

00Guide (7 patterns, DLL template)
01LRU CacheDLL + HashMap
02LFU Cache         → 2x DLL + 2x HashMap + min_freq
03Design TwitterGlobal timer + k-way heap merge
04File SystemHashMap with parent-path check
05Hit CounterCircular buffer (300 buckets)
06Time-Based KVHashMap of sorted lists + bisect
07AutocompleteTrie with per-node freq map
08Browser HistoryArray + curr index
09LeaderboardHashMap + nlargest
10Snake GameDeque + HashSet
11SkiplistProbabilistic layered list
12Log StorageArray + granularity prefix compare
13Phone DirectoryQueue + bool array
14In-Memory DBHashMap of TreeMaps
15Min StackStack + parallel min stack
16Median Finder     → max-heap lo + min-heap hi
17Circular QueueRing buffer with head/tail/size
18TinyURLCounter + base-62 + HashMap
19Parking SystemArray[3] counters
20Master RecapThis file

Common Mistakes

  1. LRU: Forgetting to delete cache[lru.key] when evicting
  2. LFU: Not decrementing min_freq correctly when freq set becomes empty
  3. Median: Pushing to hi first then rebalancing (not pushing to lo first)
  4. Hit Counter: Using < instead of <= in window check (ts - 300 < hit_ts <= ts)
  5. Skip List: Forgetting to update self.level when top levels become empty after erase

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro