LRU Cache

Least Recently Used Cache

Design a fixed-size key-value store that drops the entry nobody has touched for the longest time. Both operations must run in constant time.

Overview

A cache keeps a small number of items close at hand so that the next request for one of them is fast. It has a fixed capacity, so when it is full and a new item arrives, something has to go. The LRU rule picks the entry that was used least recently: if nobody has asked for it in a while, it is the best guess for what nobody will ask for next.

The interview version asks for two operations, get(key) and put(key, value), both in O(1) time. That constraint is the whole problem. A dict alone finds a key in one step but cannot tell you which key is oldest. A list alone keeps order but needs a scan to find a key. The classic answer combines the two: a hash map for lookup and a doubly linked list for recency order, with every node shared by both.

Interviewers use this problem to watch how you combine data structures, whether you handle pointer updates without bugs, and whether you can make a small class safe for many threads. A strong answer names the two structures in the first minute, explains why the node must carry its key, and writes the code with sentinel nodes so there are no special cases.

Scoping the problem

Before writing code, ask these six questions. Each answer changes the design a little, and the usual answer follows each one.

  • What counts as a use: reads only, or writes too? Usually both. This decides whether put on an existing key moves it to the front.
  • What does get return on a miss? It could be None, a default, or -1 as in the LeetCode version. It is the caller's choice, so the code below takes a default.
  • Is capacity fixed after construction? Usually fixed. A shrinking capacity would mean evicting several entries at once.
  • Will many threads share one cache? Usually yes, with one lock. This adds a lock and decides how fine-grained it is.
  • Do entries expire on their own (TTL)? That is a follow-up, not the core. It needs a timestamp per node and an expiry check.
  • What are the keys? Any hashable objects. The cache never inspects them.

Functional requirements

  • get(key) returns the value if the key is present, otherwise a default; a hit makes the entry the most recently used
  • put(key, value) inserts a new entry or updates an existing one, and makes it the most recently used
  • When the cache is full, put evicts the least recently used entry before inserting
  • Capacity is fixed at construction and must be positive
  • Keys may be any hashable object

Non-functional requirements

  • get and put run in O(1) time on average, including eviction
  • Memory is O(capacity): one node and one dict entry per stored key
  • Safe for concurrent get and put from many threads
  • The two internal structures are never exposed; callers see only get, put and len

Interview questions

  • Why does the list have to be doubly linked? A singly linked list is simpler.
  • How would you add a time-to-live so entries expire on their own?
  • Would you use OrderedDict or LinkedHashMap instead of writing the list yourself?
  • Your cache has one lock. How would you reduce contention with many threads?
  • When is LRU the wrong eviction policy?
  • What bugs do people write in this problem, and how do you avoid them?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio