DeepOffer

How Would You Build an O(1) Least-Recently-Used Cache?

ML CodingHot interview question
Reported in public interview compilations — Apple, Amazon, Google, Meta

Data structure combo: Hash map key to node plus doubly linked list ordered by recency. The hash map handles lookup; the list handles moves and eviction. You need both.

get: On hit, move the node to the head (most recent) and return the value. On miss, return -1.

put: If the key exists, update and move to head. If not, insert at head; if over capacity, remove the tail node and delete it from the hash map.

Python's OrderedDict does it in two lines, but interviewers default to expecting a hand-written doubly linked list. Confirm which they want first, then discuss how OrderedDict works under the follow-up.

Common follow-up questions

Practice this question with an AI interviewer

Get asked follow-ups live, then receive a scored report — like a real MLE interview loop.

Start AI mock interview