Courses › Low-Level Design › Problem Walkthroughs

Design an LRU Cache

Hash map + doubly linked list, with O(1) get/put and thread safety.

Implementing an LRU (least recently used) cache is a favorite because it sits at the boundary of data structures and design: you need the right combination of structures for O(1) operations, clean encapsulation, and — in follow-ups — thread safety.

Requirements

  • get(key) — return the value if present (and mark it most recently used), else −1 / None
  • put(key, value) — insert or update; if over capacity, evict the least recently used key
  • Both operations in O(1)

Choosing the data structures

  • A hash map gives O(1) lookup by key — but no notion of order.
  • A doubly linked list gives O(1) move-to-front and remove-from-back — if you already hold the node.

Combine them: the map stores key → node, and the list orders nodes from most to least recently used.

[Hash Map: key → node] -> (Head sentinel) -> (Most recent) -> (…) -> (Least recent) -> (Tail sentinel)

Sentinel head and tail nodes remove all the null checks for empty lists and edge positions.

Implementation

class Node:
    __slots__ = ("key", "value", "prev", "next")

    def __init__(self, key=None, value=None):
        self.key, self.value = key, value
        self.prev = self.next = None


class LRUCache:
    def __init__(self, capacity: int):
        if capacity <= 0:
            raise ValueError("capacity must be positive")
        self.capacity = capacity
        self.map = {}
        self.head, self.tail = Node(), Node()   # sentinels
        self.head.next, self.tail.prev = self.tail, self.head

    # ── list helpers ──
    def _remove(self, node):
        node.prev.next, node.next.prev = node.next, node.prev

    def _add_front(self, node):
        node.prev, node.next = self.head, self.head.next
        self.head.next.prev = node
        self.head.next = node

    # ── public API ──
    def get(self, key):
        node = self.map.get(key)
        if node is None:
            return None
        self._remove(node)
        self._add_front(node)
        return node.value

    def put(self, key, value):
        node = self.map.get(key)
        if node:
            node.value = value
            self._remove(node)
            self._add_front(node)
            return
        if len(self.map) == self.capacity:
            lru = self.tail.prev
            self._remove(lru)
            del self.map[lru.key]      # why nodes store their key
        node = Node(key, value)
        self.map[key] = node
        self._add_front(node)

[!TIP] The shortcut — and when to use it In Python, collections.OrderedDict with move_to_end and popitem(last=False) implements LRU in ten lines. Mention it, but expect the interviewer to ask for the linked-list version — that's what's being tested.

Follow-ups

Thread safety

Both get and put mutate the list, so concurrent calls can corrupt it. The simplest fix is a single lock around each method. Under heavy contention:

  • Striped / segmented caches — split keys across N independent LRU segments, each with its own lock (what many production caches do).
  • Approximate LRU — record accesses in a buffer and apply them in batches, so reads don't need the write lock.

TTL support

Store an expiry time in each node; treat expired entries as missing on get (lazy expiry), and evict them first on put.

LFU instead of LRU

Evict the least *frequently* used. Keep key → node, freq → doubly linked list, and track the current minimum frequency — still O(1).

Design for extensibility

Pull eviction out behind an EvictionPolicy interface (record_access, record_insert, evict) so the cache can switch between LRU, LFU and FIFO — the Strategy pattern again.