Skip to main content

24. LRU Cache

mediumAsked at Spotify

Design a cache that evicts the least-recently-used item when capacity is exceeded — exactly the structure Spotify uses to keep the most recently played tracks resident in memory without re-fetching from object storage.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class with a constructor that takes capacity, a get(key) method that returns the value if present (-1 otherwise) and marks it as recently used, and a put(key, value) method that inserts or updates the key and evicts the LRU item if capacity is exceeded. Both operations must run in O(1) average time.

Constraints

  • 1 <= capacity <= 3000
  • 0 <= key <= 10^4
  • 0 <= value <= 10^5
  • At most 2 * 10^5 calls will be made to get and put

Examples

Example 1

Input
LRUCache(2); put(1,1); put(2,2); get(1); put(3,3); get(2); put(4,4); get(1); get(3); get(4)
Output
[null,null,null,1,null,-1,null,1,3,4]

Explanation: After put(3,3) the cache evicts key 2 (LRU). After put(4,4) key 1 is evicted.

Approaches

1. HashMap + doubly linked list

Store nodes in a doubly linked list ordered by recency; use a hash map for O(1) key lookup. On access, move the node to the front. On eviction, remove the tail.

Time
O(1) per operation
Space
O(capacity)
class LRUCache {
  constructor(capacity) {
    this.cap = capacity;
    this.map = new Map();
    // Sentinel head and tail
    this.head = { key: 0, val: 0, prev: null, next: null };
    this.tail = { key: 0, val: 0, prev: null, next: null };
    this.head.next = this.tail;
    this.tail.prev = this.head;
  }
  _remove(node) {
    node.prev.next = node.next;
    node.next.prev = node.prev;
  }
  _insertFront(node) {
    node.next = this.head.next;
    node.prev = this.head;
    this.head.next.prev = node;
    this.head.next = node;
  }
  get(key) {
    if (!this.map.has(key)) return -1;
    const node = this.map.get(key);
    this._remove(node);
    this._insertFront(node);
    return node.val;
  }
  put(key, value) {
    if (this.map.has(key)) this._remove(this.map.get(key));
    const node = { key, val: value, prev: null, next: null };
    this._insertFront(node);
    this.map.set(key, node);
    if (this.map.size > this.cap) {
      const lru = this.tail.prev;
      this._remove(lru);
      this.map.delete(lru.key);
    }
  }
}

Tradeoff:

2. JavaScript Map (insertion-order hack)

Exploit the fact that JS Map preserves insertion order — delete and re-insert on every access to keep the oldest key at the front, evict via map.keys().next().value. Clean solution for interview speed.

Time
O(1) amortized
Space
O(capacity)
class LRUCache {
  constructor(capacity) {
    this.cap = capacity;
    this.map = new Map();
  }
  get(key) {
    if (!this.map.has(key)) return -1;
    const val = this.map.get(key);
    this.map.delete(key);
    this.map.set(key, val);
    return val;
  }
  put(key, value) {
    if (this.map.has(key)) this.map.delete(key);
    this.map.set(key, value);
    if (this.map.size > this.cap) {
      this.map.delete(this.map.keys().next().value);
    }
  }
}

Tradeoff:

Spotify-specific tips

Spotify's backend engineers treat LRU as a proxy for systems-design instinct. They want to hear you describe the sentinel-node trick that eliminates edge-case null checks, and then pivot to: 'In production, this would be backed by Redis with a TTL policy.' Mention that Spotify's audio streaming layer does exactly this — keeping recently decoded audio segments in a bounded buffer before evicting older ones.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill LRU Cache and other Spotify interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →