Design an LRU Cache
🗄️ What is an LRU cache? A cache keeps a limited number of values in fast memory so the slow source (a database, a remote service) is asked less often. When it is full and a new value arrives, something has to go. An LRU (least recently used) cache throws out the entry nobody has touched for the longest time, on the bet that recent entries will be needed again soon.
The LRU cache is one of the most asked design questions, because it sits right between a coding problem and an object-oriented one. The coding part is making every operation O(1). The design part is that interviewers rarely stop at LRU: "now make the eviction policy pluggable", "now add LFU", "now make it thread safe". Those requests are one design, not three. We build a cache whose eviction rule is a swappable policy, implement LRU and LFU (with least-recently-used as the LFU tie-breaker) behind it, and then make the whole thing safe for many threads. If you only need the LeetCode versions, they are LeetCode 146 and LeetCode 460; this post is about the design around them.
Requirements
"Design an in-memory cache with a fixed capacity. When it is full, it should evict the least recently used entry. We may want other eviction policies later."
Clarifying Questions
You: Is the capacity a number of entries or a number of bytes?
Interviewer: A number of entries. Keys and values are integers for this exercise.
You: What counts as "using" an entry? Does an update count, or only a read?
Interviewer: Both. A get that finds the key is a use, and a put that overwrites an existing key is a use. A get that misses changes nothing.
You: Which other policies should the design support?
Interviewer: LFU, least frequently used, is the one we know we need. If two keys have been used the same number of times, evict the one used least recently.
You: What complexity do you expect per operation?
Interviewer: O(1) on average for get, put and eviction, for both policies.
You: Should callers be able to delete a key? And do they need to know what was evicted?
Interviewer: Yes to both. Deleting is not an eviction. put should report which key it evicted, if any; we use that for metrics and for writing dirty values back.
You: Will several threads use the cache at once?
Interviewer: Eventually, yes: every request thread of the service. Start single-threaded, then make it safe.
You: Do entries expire after some time?
Interviewer: Not for now.
The second question is the one candidates skip. Whether an update counts as a use changes which key gets evicted, and it changes the LFU counts too. The tie-breaking rule for LFU matters just as much: "least frequently used" alone does not say which key to drop when several have the same count, and a design without a tie rule is not deterministic.
💡 Interview tip: When the interviewer says "pluggable", ask which policies they actually have in mind. "LRU now, LFU next" tells you exactly what the interface must support. Designing for ten imaginary policies is how you end up with an interface nobody can implement.
Final Requirements
- The cache holds at most
capacitykeys, each with an integer value. get(key)returns the value, or-1if the key is not cached. A hit counts as a use.put(key, value)inserts or updates. An update counts as a use and never evicts. Inserting a new key into a full cache first evicts one key, chosen by the policy, andputreturns that key (or-1).remove(key)deletes a key and returns whether it was there. It is not an eviction.- The eviction policy is chosen when the cache is created and can be swapped without touching the cache. We support LRU and LFU.
- LRU evicts the key with the oldest last use. LFU evicts the key with the fewest uses (a new key starts at 1), and among equal counts, the least recently used.
- Every operation is O(1) on average.
- The cache can be made safe for concurrent use.
Out of scope
- Expiry (time to live) and size-based capacity (we come back to both in Extensibility).
- Loading missing values from a database, and distributing the cache over several machines.
- Persistence: a cache can always be rebuilt from its source.