LRU Cache Design and Canonical Keys
Asked of: Software Engineer
Last updated
What's being tested
Candidates must show correctness and engineering rigor for an LRU cache: O(1) recency updates, deterministic memoization key construction across argument spelling/order, and safe persistence for crash resilience. Interviewers probe data-structure choice, canonicalization of function signatures, handling of non-hashable/nested inputs, and trade-offs between latency and durability.
Patterns & templates
-
Doubly-linked list + hashmap — O(1) get/set/evict; implement nodes with key/value and move-to-front on access.
-
collections.OrderedDictfor quick Python prototype —popitem(last=False)for LRU eviction, O(1) average. -
Canonical signature binding with
inspect.signature+Signature.bind— normalize positional/keyword into parameter-name order. -
Freeze supported values into immutable, type-tagged tuples (e.g.,
("list", (..)),("dict", (("k",v),..))) so lists/dicts are distinguished and hashable. -
Avoid caching exceptions — re-raise without storing failed results; only cache successful return values.
-
Crash-resilience: append-only log or write-ahead log for mutations; persist both key→value mapping and recency order (or timestamps) and fsync at chosen durability points.
-
Deterministic function identity — include function module +
__qualname__(or explicit id) in cache key to avoid cross-function collisions. -
Space/time tradeoff: persisting recency per operation increases
p99latency; batch checkpoints or asyncWALflushes reduce latency but increase potential data loss.
Common pitfalls
Pitfall: Building keys from raw
args/kwargsorder — misses canonical binding and treats equivalent calls as different.
Pitfall: Using naive
json.dumpsfor keys — loses type distinctions (tuple vs list) and can reorder dict keys unless sorted.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Debug Python LRU Cache-Key ConstructionAnthropic · Software Engineer · Onsite · hard
- Implement a Least-Recently-Used CacheAnthropic · Software Engineer · Onsite · medium
- Implement a crash-resilient LRU cacheAnthropic · Software Engineer · Onsite · medium
- Design a Crash-Resilient LRU CacheAnthropic · Software Engineer · Technical Screen · hard
- Implement a recency-eviction bounded cacheAnthropic · Software Engineer · Technical Screen · medium
- Implement crawler, dedup, and persistent LRUAnthropic · Software Engineer · Onsite · medium
- Implement Python LRU cache with args and persistenceAnthropic · Software Engineer · Onsite · medium
Related concepts
- LRU Cache Design And PersistenceCoding & Algorithms
- LRU CacheCoding & Algorithms
- LRU Cache And O(1) Data StructuresCoding & Algorithms
- Durable Key-Value Stores And CachesSystem Design
- Core Data Structures, Caches, And Clean ImplementationCoding & Algorithms
- Caching And Stateful Data Structure DesignCoding & Algorithms