Skip to problem
←

LRU Cache

Hard 0 attempts

Implement a Least Recently Used (LRU) cache with a fixed positive capacity.

  • get(key) returns the stored value and marks that key as most recently used. If absent, return -1 without changing recency.
  • put(key, value) inserts or updates the key and marks it as most recently used. Updating a key does not consume another slot. Return nothing.
  • If inserting a new key exceeds capacity, evict the least recently used key.

Both operations should take O(1) average time. Values may be 0, so a zero value must not be treated as a missing key.

For capacity 2, this sequence shows the eviction rule:

Operation Keys, least → most recent Result
put(1, 10) 1 —
put(2, 20) 1, 2 —
get(1) 2, 1 10
put(3, 30) 1, 3 evicts 2
Function contract

Implement createLRUCache(capacity), returning an object with get(key) and put(key, value) methods. Fixtures supply constructor arguments and then [method, ...arguments] calls on that object; each fixture creates a fresh cache.

Output: get returns the stored value or -1. put returns nothing (displayed as null). Results appear in operation order.

Example 1

Input
constructorArgs = [2]
operations = [["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,1,null,-1,null,-1,3,4]

Example 2

Input
constructorArgs = [1]
operations = [["put",1,1],["put",1,2],["get",1],["put",2,3],["get",1],["get",2]]
Output
[null,null,2,null,-1,3]
Constraints
  • 1 <= capacity <= 3000
  • 0 <= key <= 10^4
  • 0 <= value <= 10^5
  • At most 2 * 10^5 calls

Approach: Hash Map and Doubly Linked List

Map each key to its list node. Keep the least recently used entry next to a head sentinel and the most recently used entry next to a tail sentinel. A successful read or update unlinks its node and appends it before the tail. For a new key, append a node; if capacity is exceeded, remove the node after the head and delete its map entry.

Sentinels keep empty-list and endpoint operations uniform. Unlinking and appending use a fixed number of pointer changes; no list scan or array shift is needed.

javascript
function createLRUCache(capacity) {
  const entries = new Map();
  const head = { next: null };
  const tail = { prev: head };
  head.next = tail;

  function unlink(node) {
    node.prev.next = node.next;
    node.next.prev = node.prev;
  }

  function append(node) {
    node.prev = tail.prev;
    node.next = tail;
    tail.prev.next = node;
    tail.prev = node;
  }

  return {
    get(key) {
      const node = entries.get(key);
      if (!node) return -1;
      unlink(node);
      append(node);
      return node.value;
    },
    put(key, value) {
      let node = entries.get(key);
      if (node) {
        node.value = value;
        unlink(node);
      } else {
        node = { key, value, prev: null, next: null };
        entries.set(key, node);
      }
      append(node);
      if (entries.size > capacity) {
        const oldest = head.next;
        unlink(oldest);
        entries.delete(oldest.key);
      }
    },
  };
}

Time Complexity: O(1) average per operation, assuming average O(1) hash-map access.

Space Complexity: O(capacity) for the map and list.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript