Skip to the guide

Design an LRU cache

IntermediateCase study11 pages

The interviewer asks you to build a cache with a fixed capacity, where get and put both run in O(1), and the least recently used key leaves when the cache is full. Picture a lawyer's desk with room for a pile of three case files. Each file has a coloured tab sticking out of its side, so she can find any file at once and slide it out. The file she just used goes on top, and when a fourth arrives, the file at the bottom goes back to the archive.

It is also asked as "LeetCode 146" or "a thread-safe LRU cache". The closest other case study is the key-value store, which keeps every key until you delete it. Interviewers like this question because the whole round goes on two structures that must agree and a read that secretly writes. This page is the class-level part. The rules themselves live on Eviction: LRU, LFU and TTL, and the cache as a service on many machines lives on Distributed cache.

Below is a full 45-minute round on the LLD framework. You are the candidate. At each Guess before you read on, stop and answer first. The full runnable project, with its tests, is a download in Java, JavaScript and Python.

Run the full project

This design as a project you can run, with its tests and a demo. You only need Java 21, Node 24 or Python 3.13. The README says how to run it.

Step 1 · Clarify the requirements and cut the scope

Minutes 0–5 · How this step works

Interviewer: Build an LRU cache.

You: What are the keys and values, and what does the capacity count?

Interviewer: String keys and any value. The capacity counts entries, and it's 100,000.

You: Does a put count as a use, or only a get?

Interviewer: Both. Keys don't expire for now.

You: Do many threads share one cache?

Interviewer: Yes, a server's request threads. Most calls are get.