Medium · Design · spoken drill
Free AI LeetCode Communication Coach — LRU Cache
Map key → node, doubly linked list for recency; get/put both O(1).
The problem
Design a data structure that follows LRU eviction. get and put must both run in O(1) average time.
Example: capacity 2; put 1; put 2; get 1; put 3 (evicts 2); get 2 → -1 → LRU order updates on each get/put
Map + list — say both roles.
# sketch: OrderedDict move_to_end + popitem(last=False)
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity):
self.cap = capacity
self.od = OrderedDict()
def get(self, key):
if key not in self.od: return -1
self.od.move_to_end(key)
return self.od[key]
def put(self, key, value):
if key in self.od: self.od.move_to_end(key)
self.od[key] = value
if len(self.od) > self.cap: self.od.popitem(last=False)How to explain it
1. Restate
Say the problem in your own words.
One or two sentences. Show you understood the input, the output, and the goal — not that you memorised the prompt.
2. Approach
Name the method before you code.
Brute force first if you need it, then the structure you will use: hash map, two pointers, stack, binary search.
3. Example
Walk one concrete input.
Pick small numbers. Say what you store, what you compare, and what you return. Interviewers follow an example more easily than abstract talk.
4. Time and space
One sentence each.
After the example, before you claim you are done. “Time is O(n) because we scan once. Space is O(n) for the map.”
5. Edge cases
Name at least one unusual input.
Empty input, duplicates, already sorted, overflow. Invite a follow-up: “I would also check …”
- “Hash map for O(1) lookup, list for recency order.”
- “On access I move the node to the front.”
Practise out loud
3 free scored runs left this hour.
Problem · Medium
Design a data structure that follows LRU eviction. get and put must both run in O(1) average time.
Example: capacity 2; put 1; put 2; get 1; put 3 (evicts 2); get 2 → -1 → LRU order updates on each get/put
- 1Restate — Say the problem in your own words.
- 2Approach — Name the method before you code.
- 3Example — Walk one concrete input.
- 4Time and space — One sentence each.
- 5Edge cases — Name at least one unusual input.
Hit record. Short countdown, then 60–90 seconds. We score the five-step script — never pronunciation.
Recording needs Chrome or Edge. You can still type below.
Model spoken script (~75s)
1. Restate
I need get and put in constant time, and when the cache is full I evict the least recently used key.
2. Approach
I pair a hash map from key to node with a doubly linked list ordered by recency. On get or put I move the node to the front. On overflow I remove the tail.
3. Example
Capacity two: put 1, put 2, get 1 moves 1 to front, put 3 evicts 2. Get 2 then returns minus one.
4. Time and space
Get and put are O(1) average. Space is O(capacity).
5. Edge cases
I would check capacity one and updating an existing key without growing size.
How to explain LRU Cache out loud, step by step
In one sentence: You have to build a fixed-size key-value store that, when it's full, throws out the entry that was used longest ago, with fast reads and writes.
What to say, in order
- “I need get and put to both run in constant time, and when the cache is over capacity I evict the least recently used key.”
- “A hash map alone gives fast lookup but no order. A list alone gives order but slow lookup. So I'll combine them.”
- “I keep a doubly linked list ordered by recency: most recently used at the front, least recently used at the back.”
- “The hash map goes from key to the list node, so I can find any node in O(1).”
- “On get, if the key exists, I move its node to the front and return the value. On put, I update or insert the node at the front.”
- “If the size goes over capacity, I remove the node at the back and delete its key from the map. I use dummy head and tail nodes so I never handle null neighbours.”
Key English phrases for LRU Cache
- least recently used
- — the entry whose last access is oldest
- evict
- — remove an entry to make room
- doubly linked list
- — each node knows both its previous and next node
- sentinel or dummy node
- — a fixed head and tail that simplify edge cases
- move to front
- — mark an entry as most recently used
How to say the time and space complexity
“Both get and put are O(1) on average: the hash map finds the node, and the doubly linked list lets me unlink and relink it in constant time. Space is O(capacity) for the map and the nodes.”
Say why the list must be doubly linked: removing a node in O(1) needs its previous pointer.
Communication mistakes on LRU Cache
- Mistake: Saying 'a queue' for the recency order. Instead: A queue can't remove from the middle in O(1). Say 'doubly linked list'.
- Mistake: Forgetting that a get also counts as a use. Instead: Say 'both get and put move the key to the front.'
- Mistake: Not storing the key inside the node. Instead: Say 'each node stores its key, so on eviction I can delete it from the map.'
- Mistake: Saying 'I just use OrderedDict' and stopping. Instead: Mention it as a shortcut, then explain the structure underneath it.
Likely follow-up questions
Interviewer: “How would you make it thread-safe?”
You: “The simplest correct option is one lock around get and put, because even a get changes the list order. For more throughput I could shard the cache by key, with a lock per shard.”
Interviewer: “How would you change it to least frequently used?”
You: “I'd track a use count per key and keep a map from each count to a list of keys, plus the current minimum count. Evict from the minimum-count list, breaking ties by recency.”
Common mistakes when explaining LRU Cache
- Saying hash map alone without the list for order.
- Not narrating eviction when capacity is full.
Other problems
Easy · Hash map
Two Sum
Classic hash-map scan. Name brute force, then the map, walk [2,7,11,15] target 9, finish with O(n).
Medium · Hash map
Group Anagrams
Group strings by anagram signature — sorted key or 26-letter count — then walk one bucket.
Easy · Two pointers
Valid Palindrome
Two pointers from both ends; skip junk characters; compare lowercase letters.
Medium · Two pointers
3Sum
Sort, fix i, then two pointers for the pair that sums to −nums[i]. Deduplicate out loud.
Medium · Sliding window
Longest Substring Without Repeating Characters
Grow a window; when a duplicate appears, move the left edge past its last index.
Easy · Stack
Valid Parentheses
Stack story: push openers, pop on matching closer, empty stack at the end.
FAQ
Questions
More questions? Email us at contact@mocklyenglish.com.
Course: How to explain a LeetCode solution · Think out loud · Explain code out loud