Skip to main content

146. LRU Cache

mediumAsked at GoDaddy

Design a data structure that evicts the least-recently-used entry when capacity is hit — this is the canonical DNS resolver cache problem that GoDaddy's infrastructure team asks to assess whether you can build O(1) get and put using a doubly-linked list plus a hash map.

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

Problem

Design a data structure that follows the Least Recently Used (LRU) cache constraints. Implement the LRUCache class with a constructor that sets capacity, a get(key) method returning the value or -1, and a put(key, value) method that inserts or updates the key and evicts the LRU entry when over capacity. 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 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), key 2 is evicted (LRU). After put(4,4), key 1 is evicted.

Approaches

1. Ordered map (JS Map insertion order)

JavaScript's Map preserves insertion order; delete-and-reinsert on access simulates LRU, and the first key is always the eviction candidate.

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:

2. Doubly-linked list + hash map

Explicit DLL with sentinel head/tail nodes; hash map stores node references for O(1) pointer surgery on every access or eviction.

Time
O(1)
Space
O(capacity)
class LRUCache {
  constructor(capacity) {
    this.cap = capacity;
    this.map = new Map();
    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:

GoDaddy-specific tips

GoDaddy's infra interviews treat LRU Cache as a proxy for distributed-systems thinking — explain how you'd extend this to a multi-node DNS cache with consistent-hashing shards. Mention that the JS Map shortcut is fine in interviews but you'd reach for the explicit DLL in a production cache layer.

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 GoDaddy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →