LRU Cache Design And Persistence
Asked of: Software Engineer
Last updated

What's being tested
This tests LRU cache implementation with O(1) lookup, update, and eviction using a hash map plus doubly linked list. Harder variants add memoization key canonicalization, variable *args/**kwargs, and persistence/crash recovery without losing ordering correctness.
Patterns & templates
-
Hash map + doubly linked list — map keys to nodes; list order stores recency;
get/putmove nodes to front inO(1). -
Sentinel head/tail nodes simplify
remove(node)andinsert_front(node); avoid special cases for empty, one-item, and tail eviction. -
Capacity eviction happens after insert/update; if
size > capacity, removetail.prevand delete its key from the map. -
Decorator memoization wraps
func(*args, **kwargs); key should include function identity plus canonicalized arguments, not just raw positional tuple. -
Canonical argument binding with
inspect.signature(func).bind()normalizes defaults and keyword order; convert unhashable structures recursively before hashing. -
Persistence snapshot serializes capacity, key-value pairs, and recency order using
pickle,json, or custom encoding; restore list order exactly. -
Crash resilience needs atomic writes: write to temp file,
flush/fsync, thenos.replace; optionally use an append-only log plus compaction.
Common pitfalls
Pitfall: Updating a value without moving it to most-recent breaks the LRU contract; both cache hits and overwrites count as use.
Pitfall: Using
str(args) + str(kwargs)for keys is nondeterministic or ambiguous; keyword order and mutable containers must be canonicalized.
Pitfall: Persisting only the dictionary is insufficient; recovery also needs recency order, capacity, and enough metadata to reject corrupted snapshots.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Implement a Least-Recently-Used CacheAnthropic · Software Engineer · Onsite · medium
- Implement a crash-resilient LRU cacheAnthropic · Software Engineer · Onsite · none
- 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 CacheCoding & Algorithms
- LRU Cache And O(1) Data StructuresCoding & Algorithms
- Caching And Stateful Data Structure DesignCoding & Algorithms
- Durable Key-Value Stores And CachesSystem Design
- Mutable Data Structures And O(1) DesignCoding & Algorithms
- Linked Lists, Pointers, Caches, And In-Memory StoresCoding & Algorithms