Skip to content

Start typing to search patterns, problems and pages.

Design

Think of a cloakroom. Your ticket number takes the attendant straight to your coat, and the rail keeps coats in the order they were handed in. Two simple tools, each answering a different question fast.

⏱ about 15 minutesNeeds: hash maps, pointersTime O(1)Space O(n)

2 · Watch

Design in 60 seconds

A cache holds 2 keys, and when it fills, the key used longest ago has to go. Keep the keys in a plain list and every call scans it to find one.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

LRU Cache

or try
A capacity of 1–4, then | and up to 10 calls like put 1 10 or get 1, split by commas. Keys 0–9.
Capacity 2. An empty map, and a list that runs from oldest to newest.Step 1 of 13. Capacity 2. The cache is empty.
Step 1 of 13
solution.ts
class Node {
prev: Node | null = null;
next: Node | null = null;
constructor(public key: number, public value: number) {}
}
class LRUCache {
private map = new Map<number, Node>();
private head = new Node(0, 0); // oldest end
private tail = new Node(0, 0); // newest end
constructor(private capacity: number) {
this.head.next = this.tail;
this.tail.prev = this.head;
}
get(key: number): number {
const node = this.map.get(key);
if (!node) return -1;
this.remove(node);
this.append(node); // now the newest
return node.value;
}
put(key: number, value: number): void {
const old = this.map.get(key);
if (old) this.remove(old);
const node = new Node(key, value);
this.map.set(key, node);
this.append(node);
if (this.map.size > this.capacity) {
const oldest = this.head.next!;
this.remove(oldest);
this.map.delete(oldest.key);
}
}
private remove(node: Node): void {
node.prev!.next = node.next;
node.next!.prev = node.prev;
}
private append(node: Node): void {
node.prev = this.tail.prev;
node.next = this.tail;
this.tail.prev!.next = node;
this.tail.prev = node;
}
}
▶ marks the line running now

4 · Your turn

0 of 4 problems solved

Start from this

class Cache {
  // One structure finds things fast...
  private byKey = new Map<number, Node>();
  // ...and another keeps them in the order you need.
  private order = new DoublyLinkedList();

  use(key: number): void {
    const node = this.byKey.get(key);
    if (!node) return;
    // Every call touches both, in O(1) each.
    this.order.moveToEnd(node);
  }
}

Easy ways to go wrong

  • Updating one structure and forgetting the other, so the map points at a node the list dropped.
  • Scanning an array to find or remove a value, which makes the call O(n).
  • Skipping the head and tail sentinels, then special-casing the empty list everywhere.
  1. Medium01

    Min Stack

    LeetCode 155 · NeetCode 150

    Pair each value with the smallest value below it.

  2. Medium02

    LRU Cache

    LeetCode 146 · NeetCode 150

    The one you just watched. Write it from memory.

    Watch it run
  3. Medium03

    Time Based Key-Value Store

    LeetCode 981 · NeetCode 150

    A map of lists, then a binary search inside one list.

  4. Medium04

    Insert Delete GetRandom O(1)

    LeetCode 380

    Swap the value you remove with the last one, so the array never has gaps.

UP NEXT · LESSON 9 OF 20TreesRecursion that follows the branches.