How do you design an LRU cache with O(1) get and put?
PICTURE THIS: HASH MAP
Simple meaning
Hash map from key to node plus a doubly linked list of recency: head is most recent, tail is least.
WHY — Hashing instead of guessing?
Why interviewers care about Hashing:
question about Hashing.
trade-offs, and what you would actually do on a DSA project - not buzzwords.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1Hash map from key
to node plus a doubly linked list of recency: head is most recent, tail is least.
- 2Get moves a node
to the head
- 3put inserts at head
and evicts the tail when over capacity.
- 4Hash and list updates
are O(1)
- 5Common mistake
space is O(capacity).
- 6A Python OrderedDict is
the same idea.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
Hash map from key to node plus a doubly linked list of recency: head is most recent, tail is least. Get moves a node to the head