
Understand it
Explain why a hash map alone cannot evict the least recent key in O(1). The usual design pairs lookup with an order structure and makes capacity, ownership, and mutation costs explicit.
An LRU cache evicts the least recently used entry when capacity is full. A hash map gives fast lookup by key but does not by itself maintain recency order. A doubly linked list or another order structure can move an accessed node to the most-recent end in O(1), while the map locates the node in expected O(1). The design becomes tricky at the boundary between the map and the list: both must refer to the same entry without leaving stale links or violating Rust's ownership rules.
In Rust, one practical solution is an indexed arena: store nodes in a Vec or slab, keep indices in the map, and maintain previous/next indices. Reuse removed slots carefully so stale indices cannot target a new node without validation. Another option is a well-tested library when the interview is about product design rather than pointer mechanics. Define capacity zero, replacement of an existing key, and whether a read updates recency. Eviction should remove the map entry and unlink the node together.
Trace a cache of capacity two through put A, put B, get A, put C. The expected eviction is B. Then test updating A and inserting when capacity is zero. State that hash-map operations are expected O(1), while a balanced-tree alternative changes bounds and memory costs. If concurrent access matters, discuss a lock or sharding and whether recency updates on every read create contention. A strong answer names invariants and edge cases before optimizing.
Interview follow-up: What happens at capacity zero, when a key is updated, and when an arena slot is reused? Explain how stale node identities are prevented.
Read the code
struct Lru<K, V> {
capacity: usize,
// map: K -> node
// order: most to least recent
marker: std::marker::PhantomData<(K, V)>,
}Design sketch, not a complete O(1) cache implementation; map lookup and stable list nodes must maintain one recency invariant.
Practice starter
// Design sketch: map K -> node ID, plus a doubly linked recency list.
// get/put moves one existing node; eviction removes the tail.Try it
Trace a capacity-two cache through put A, put B, get A, put C. Name the evicted entry and each structure that changes.
What success looks like: You name B as the eviction and explain how the map and the recency list stay in step.
Check your understanding
Which invariant supports O(1) expected lookup and recency updates in a full LRU design?
Reveal the answer
- The map locates a node directly and the list unlinks/relinks that node in constant time.
- A HashMap alone stores a permanent total order of most-recent use.
- A VecDeque scan gives worst-case O(1) lookup for arbitrary keys.
A standard VecDeque prototype can illustrate policy with O(n) scans. A production O(1)-update design also needs valid stable node IDs, edge handling and a capacity contract.
Go deeper
Common misconception: A HashMap alone stores a permanent total order of most-recent use.
A HashMap records no recency order, and its iteration order is unspecified. An LRU needs a separate structure that tracks use, such as a linked list.
- Identify the contract: Combine lookup speed with recency updates.
- Trace the concrete example: The sketch names two jobs: the map finds a node and the order tracks recency. Neither alone gives O(1) eviction.
- Test the changed constraint: What happens at capacity zero, when a key is updated, and when an arena slot is reused? Explain how stale node identities are prevented.
Transfer challenge: What happens at capacity zero, when a key is updated, and when an arena slot is reused? Explain how stale node identities are prevented.
Connections
- Live Coding: Two Sum: Clarify duplicates, indices, and complexity first
- Design a Parser: Separate tokenization, grammar, and diagnostics
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Design an LRU Cache (courseId "interview", lessonId "q-lru-design") Link: https://crabrix.com/learn/interview/q-lru-design/ Goal: Explain why a hash map alone cannot evict the least recent key in O(1). Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-lru-design) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.