Design
Pattern: combine basic structures so that every operation hits its target complexity.
| Need | Combination |
|---|---|
| O(1) get + O(1) recency update | hash map → node in a doubly linked list (LRU) |
| O(1) insert / delete / random | array + value→index map, swap with last on delete |
| Frequency buckets | map key→freq, map freq→ordered set, track minFreq (LFU) |
| Versioned values | per-key list of (version, value) + binary search |
| FIFO with stacks | two stacks, move lazily (amortized O(1)) |
LRU: map {k -> node}
head <-> [most recent] <-> ... <-> [least recent] <-> tail
get/put: move node to the front; evict tail.prev when over capacity
Other design problems elsewhere: Min Stack (155), Time Map (981), Twitter (355), Median Finder (295), Trie (208), BST Iterator (173), Serialize Tree (297).
Problems
Showing 19 of 19