- 18 of 909 reports mention it: Amazon 5, Meta 5, Anthropic 2, LinkedIn 2, plus one each from DoorDash, Apple, Confluent and Salesforce. 4 are from the last 90 days, 3 of them Amazon.
- The answer is a hash map from key to node, plus a doubly linked list ordered by recency. Both operations are O(1).
- Both get and put count as a use. Forgetting to refresh on get is the most common bug.
- Expect a follow-up: TTL, thread safety, keys built from function arguments, or persistence. Anthropic and LinkedIn reports describe multi-part versions.
- In AI-allowed rounds you still own the design and the review. Test against a simple reference before you trust generated pointer code.
The problem
You are given a capacity. Build a key-value store with two operations: get(key) returns the stored value, or -1 if the key is absent, and put(key, value) inserts or updates a value. When an insert would push the store past its capacity, first remove the entry that has gone the longest without being read or written. Both operations should run in O(1) average time.
"Used" means either a successful get or a put. Updating an existing key refreshes it too.
Examples
Capacity 2. The order column lists keys from most to least recently used.
| Call | Returns | Order after the call | Note |
|---|---|---|---|
| put("a", 1) | a | ||
| put("b", 2) | b, a | ||
| get("a") | 1 | a, b | Reading a makes it most recent |
| put("c", 3) | c, a | Full, so b (least recent) is evicted | |
| get("b") | -1 | c, a | Miss |
| put("a", 10) | a, c | Update counts as a use | |
| put("d", 4) | d, a | c is evicted, not a | |
| get("a") | 10 | a, d |
Who asks it
We coded 909 first-hand candidate reports that list questions, dated January 2025 to November 2026. 18 mention an LRU cache. Counting only single-company reports: Amazon 5, Meta 5, Anthropic 2, LinkedIn 2, and one each from DoorDash, Apple, Confluent and Salesforce. 4 of the 18 are from the last 90 days. That is more than most named LeetCode-style problems in our data, where recent counts are often zero.
| Company | What candidates describe |
|---|---|
| Amazon | New-grad loop, Oct 2026: an "LRU prompt store", alongside a rate limiter and an object design question [1]. Graduate role in London, Sep 2026: an LRU-style hash map plus doubly linked list [2]. SDE1, Sep 2026: LRU cache [4]. |
| Apple | Phone screen, Sep 2026: LRU cache, solved bug-free on the first run; the candidate was still rejected [3]. |
| Anthropic | Senior onsite, Jul 2026: LRU cache keyed by function arguments, then crash recovery from a file log; the candidate ran out of time with tests failing [8]. An Aug 2025 report: a Python LRU cache handling args and kwargs, with a persistence follow-up [9]. |
| AI-assisted coding, Aug 2026: LRU, then LRU with TTL, then thread safety, with AI writing the code [5]. | |
| Meta | Five reports, all from the first half of 2025, including an LRU with extra constraints in a phone screen [13]. |
| DoorDash | E4 phone screen, 2025: an LRU variant framed around dasher assignments [10]. |
Three of the four recent reports are Amazon new-grad or SDE1 loops. For senior roles, the recent signal is the multi-part version (Anthropic, LinkedIn), not the plain problem.
Approach: brute force to optimal
Start by naming what each operation must do: find a key fast, move it to the "most recent" end, and remove from the "least recent" end. Each candidate structure fails at one of these until you combine two.
| Approach | get | put / evict | Why it falls short |
|---|---|---|---|
| List of (key, value) pairs in recency order | O(n) to find | O(n) to move or remove | Every operation scans |
| Hash map + last-used timestamp per key | O(1) | O(n) eviction: scan for the oldest timestamp | Eviction scans all keys |
| Hash map + min-heap of timestamps (lazy deletion) | O(log n) | O(log n) | Works, but not O(1), and stale heap entries pile up |
| Hash map + doubly linked list | O(1) | O(1) | The standard answer |
| Python OrderedDict | O(1) | O(1) | Correct, but many interviewers then ask you to build the list yourself |
The list has to be doubly linked because removing a node from the middle needs its predecessor; with a prev pointer you unlink in O(1). Each node also stores its key. When you evict the tail node, you must delete it from the hash map too, and the node is the only place you have the key.
Two sentinel nodes (a dummy head and tail) remove all the empty-list and single-node edge cases from the pointer code.
Python solution
class Node:
__slots__ = ("key", "val", "prev", "next")
def __init__(self, key=0, val=0):
self.key, self.val = key, val
self.prev = self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.map = {} # key -> Node
self.head, self.tail = Node(), Node() # sentinels
self.head.next, self.tail.prev = self.tail, self.head
def _unlink(self, node):
node.prev.next, node.next.prev = node.next, node.prev
def _push_front(self, node): # front = most recently used
node.prev, node.next = self.head, self.head.next
self.head.next.prev = node
self.head.next = node
def get(self, key: int) -> int:
node = self.map.get(key)
if node is None:
return -1
self._unlink(node)
self._push_front(node)
return node.val
def put(self, key: int, value: int) -> None:
if self.cap <= 0:
return
node = self.map.get(key)
if node:
node.val = value
self._unlink(node)
self._push_front(node)
return
if len(self.map) == self.cap:
lru = self.tail.prev # least recently used
self._unlink(lru)
del self.map[lru.key]
node = Node(key, value)
self.map[key] = node
self._push_front(node) Get and put take O(1) average time. Space is O(capacity).
The short version with collections.OrderedDict uses move_to_end(key) on every use and popitem(last=False) to evict. Offer it first if you like, then say you can build the linked list by hand. Ask which the interviewer wants.
Follow-up questions
- TTL per entry (LinkedIn's second part [5]): store an expiry time in each node. On get, treat an expired entry as a miss and remove it. For proactive cleanup, keep a min-heap of expiry times or sweep periodically. Say which entries are evicted first when both rules apply.
- Thread safety (LinkedIn [5][6]): a single lock around get and put is correct and simple. Note that get also mutates the list, so a read-write lock does not help much. For high contention, shard the cache into N independent LRUs by key hash, each with its own lock.
- Keys from function arguments (Anthropic [8][9]): build a memoizing decorator. The key is the positional args plus the keyword args sorted by name, which only works if every argument is hashable. Say what you do when one is not.
- Persistence and crash recovery (Anthropic [8]): append every put and eviction to a log file, replay it on startup, and compact the log from time to time by writing a snapshot. Discuss flushing and partial last lines.
- LFU instead of LRU: evict the least frequently used key, breaking ties by recency. The O(1) version keeps a map from frequency to its own linked list, plus the current minimum frequency.
- Size by bytes instead of count: track total bytes and evict from the tail in a loop until the new entry fits.
- Distributed cache: split keys across nodes with consistent hashing; each node runs its own LRU.
The AI-allowed version
LRU cache is a common task in AI-assisted rounds because the code is short but the pointer logic is easy to get subtly wrong. Several reports describe it:
One LinkedIn candidate (Aug 2026) worked in CoderPad with a choice of models. The parts went LRU, LRU with TTL, a third step, then thread safety. The algorithm had to come from the candidate; the AI was for implementation. They reviewed generated code line by line, finished 2 of 4 parts, and wrote: "I could have finished these problems by typing the code myself; using AI made it slower." [5] A commenter in a LinkedIn IC2 thread described the same shape: the AI writes the LRU, the candidate finds its bugs, then adds thread safety [6]. A LinkedIn AI engineer report from Dec 2025 had an LRU cache with feature additions done by prompting [7].
Meta's careers FAQ says many interviews now include a built-in AI assistant that candidates are expected to use [11]. A Meta E6 candidate was rejected after not reviewing AI-generated code carefully and missing a bug before moving on to optimization [12].
- You still pick the structure and explain why it is O(1). You state the invariants (map and list hold the same keys, the head side is most recent, size never exceeds capacity), decide the TTL and eviction rules, and reason about locking.
- In generated code, check that get refreshes recency, that updating an existing key does not create a second node, and that eviction deletes from the map too. Also try capacity 0 and 1, and confirm unlink happens before relinking.
- To verify fast, write five targeted asserts first; the calls in the examples table make a good set. Then run a short random test that applies the same operations to your cache and to an
OrderedDictreference and compares every result. It takes two minutes to write and catches almost every pointer bug. - Narrate the review. Interviewers in these rounds watch how you check the AI's work as well as whether the final code passes.
Common mistakes
- Not moving the key to the front on get.
- On put of an existing key, inserting a new node instead of updating and moving the old one, which leaves a ghost node in the list.
- Evicting before checking whether the key already exists, which throws out an entry for no reason.
- Removing the tail node from the list but not from the map (or not storing the key in the node, so you cannot).
- Using a singly linked list or
list.remove, which makes removal O(n). - No sentinels, then a tangle of None checks that hides a bug.
- Ignoring capacity 0.
How to prepare
Write it cold in 15 minutes
Hand-built linked list version, in a plain editor, no autocomplete. Then the OrderedDict version in two.
Do one follow-up per day
TTL, thread safety, argument-based keys, and persistence. Anthropic and LinkedIn reports describe these as later parts of one problem.
Practise the review
Ask an AI tool for an LRU cache, then find its bugs with the random test against OrderedDict. That is the skill AI-assisted rounds test.
Practise LRU cache with follow-ups
A mock coding interview that adds TTL and thread safety after the base version.
Start free mockFAQ
Is OrderedDict acceptable?
It is correct and O(1). Many interviewers accept it as a first answer and then ask you to build the linked list yourself. Ask early which they want.
Can I use a plain Python dict, since dicts keep insertion order?
Yes, as a trick: to refresh a key, pop it and insert it again; to evict, remove next(iter(d)). Both are O(1) on average. Explain why it works, since the interviewer may not expect it.
Why not a heap?
A heap gives O(log n), not O(1), and updating a key's position needs lazy deletion. The linked list gives O(1) for every step.
Which companies ask LRU cache now?
In our data: Amazon 5, Meta 5, Anthropic 2, LinkedIn 2, and one each from DoorDash, Apple, Confluent and Salesforce. 4 reports are from the last 90 days, 3 of them Amazon [1][2][4].
How do I make it thread-safe without one global lock?
Split the cache into N shards by key hash, each with its own LRU and lock. Eviction then becomes per shard, which is approximate LRU overall. Say that trade-off out loud.
Sources
- Candidate report · Reddit r/leetcode: Amazon SDE 1 in-person loop, fall 2026 (LRU prompt store) · Oct 28, 2026
- Candidate report · Reddit r/leetcode: Amazon London SDE graduate interview experience · Sep 23, 2026
- Candidate report · PracHub: Apple SWE phone screen, rejected after a bug-free LRU · Sep 2026
- Candidate report · Reddit r/leetcode: Amazon SDE1 interview experience 2026 · Sep 11, 2026
- Candidate report · PracHub: LinkedIn SWE, two of four AI coding tasks completed (LRU variants) · Aug 2026
- Candidate report · Reddit r/leetcode: LinkedIn IC2 software engineer interview (AI round discussion) · Sep 9, 2026
- Candidate report · LeetCode Discuss: LinkedIn AI engineer IC2 virtual onsite, AI coding round with LRU · Dec 18, 2025
- Candidate report · PracHub: Anthropic senior SWE onsite, LRU with crash recovery · Jul 2026
- Candidate report · Hacker News: Anthropic interview experience (Python LRU cache with args/kwargs) · Aug 2025
- Candidate report · LeetCode Discuss: DoorDash E4 phone screen, LRU variant · Apr 2025
- Official · Meta Careers: hiring process FAQ (built-in AI assistant)
- Candidate report · PracHub: Meta E6, rejected after missing a bug in AI-assisted coding · Nov 2026
- Candidate report · LeetCode Discuss: Meta E4/E5 phone screen, LRU with extra constraints · May 2025
We quote briefly, never name posters, and correct errors on request: report an error.