System / DS Design
O(1) target ops
Combine two structures so each pulls its weight: hashmap for lookup + linked list/heap/array for ordering.
0/21 solved
Recognise it
The signal
"Design a class" with O(1) ops: LRU/LFU cache, min stack, insert-delete-getRandom, time-based store, iterator.
1Brute force
One structure with linear scans for some operations.
2The observation
The slowest operation: add an auxiliary structure that indexes exactly what that operation needs.
3Why it works
Every auxiliary structure (map, list, heap, array) stays consistent with the others after every operation.
Reach for it when
- +'Design a class' with strict O(1) (or O(log n)) operations — caches, min stack, random-access set.
- +You need two capabilities at once (fast lookup + ordering/min/random) no single structure provides.
- +Stateful data structures backing a system (rate limiter, time-store).
Not when
- ×A single standard container already does the jobuse That container; don't over-engineer
- ×It's an algorithm question in disguise (graph/DP)use Solve the underlying algorithm
- ×Persistence or concurrency requirementsuse Out of scope for in-memory DS designs
- ×Requirements contradict (O(1) sorted iteration + O(1) arbitrary insert)use Point out the conflict; ask which op may relax
Intuition
"Design a class where every operation is O(1)" problems are about combining two simple structures so each covers the other's weakness. A hash map gives O(1) lookup but no order; a linked list / array / heap gives order but slow lookup. Glue them together and you get both.
Read the full intuition
LRU Cache is the canonical one. You need O(1) get/put and you must evict the least-recently-used item — so you need to know usage order. Recipe:
- A hash map: key → node, for O(1) lookup.
- A doubly linked list of nodes in usage order: most-recent at the front, least-recent at the back.
- On
get/put: find the node via the map, unlink it and move it to the front (now most-recent). On overflow, drop the tail (least-recent) and delete its key from the map.
Why a doubly linked list? Because moving a node to the front requires unlinking it in O(1), which needs both its prev and next. Dummy head & tail sentinels remove all the null-edge-case branches.
Other combos: Min Stack = a normal stack plus a second stack tracking the running min. Insert/Delete/GetRandom O(1) = a dynamic array (for random pick) + a map value→index (swap the removed element with the last, then pop). Time-Based Store = map key → sorted list of (timestamp, value) + binary search.
Key idea
The interview skill: name the operations and their required time, then ask "what structure gives O(1) lookup? what gives me the ordering/min/random I need?" — and bolt them together.
Why it costs what it costs
Time O(1) per op. hash lookup is O(1) and linked-list splice / array swap are O(1); time-store is O(log n) via binary search.
Space O(capacity). stores at most the cache/structure size in the map plus the ordering structure.
Practice
- Implement Queue using StacksFoundationFoundationMeta, AmazonEasy
- Min StackApply it · Augment with extra info.Apply itAmazon, UberMedium
- Time Based Key-Value StoreOne change · Design + binary search.One changeStripe, GoogleMedium
- Insert Delete GetRandom O(1)One change · O(1) delete in an array.One changeMeta, AmazonMedium
- LRU CacheTwists · O(1) recency updates.TwistsAmazon, MetaMedium
- Design TwitterCombine · Feed merge.CombineNetflix, CREDMedium
- LFU CacheAdvanced · Two-level ordering (frequency, recency).AdvancedAmazon, UberHard
- All O`one Data StructureMastery checkMastery checkHard
- Implement Stack using QueuesMicrosoft, AdobeEasy
- Moving Average from Data StreamPremiumEasy
- Number of Recent CallsStripe, UberEasy
- Design Circular DequeGoogle, UberMedium
Pitfalls
Classic bugs
- !LRU with a singly linked list can't unlink in O(1): use a doubly linked list (or an ordered map).
- !No dummy head/tail sentinels: null checks everywhere and off-by-ones at the ends.
- !LRU
getmust also move the node to most-recent: a read counts as a use. - !GetRandom delete: swap with the last element, pop, and update the moved element's index in the map.
- !Min Stack: store the running min with each element (or a second stack) so
poprestores the old min. - !JS Map LRU: re-insert on every touch;
keys().next().valueis the least recently used.
Go deepervariants and techniques · 1 combinations · resources
Variants and techniques
- LRUHash map + doubly linked list (or ordered map); evict tail. LRU Cache.
- LFUMap key→node, map freq→list, track minFreq; bump frequency on access. LFU Cache.
- Min/Max stackAuxiliary stack of running extrema alongside the main stack. Min Stack.
- O(1) random setArray + value→index map; swap-with-last to delete. Insert Delete GetRandom O(1).
- Time-versioned storeMap key→sorted (ts,val); binary search the timestamp. Time Based Key-Value Store.
- Queue via stacksTwo stacks (in/out); amortized O(1) by lazy transfer. Implement Queue using Stacks.
Combinations
Hash Map / Hash Set Lookup+System / DS Design
LRU/LFU caches
- Hash Map / Hash Set Lookup:
- maps each key to its list node in O(1)
- System / DS Design:
- a doubly linked list spliced in O(1) keeps recency order (LFU: one list per frequency)