LRU cache with hashing and linked list: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Find entries through hash bucket chains and move accessed nodes to the head of a doubly linked recency list. Evict the tail on a full-cache miss.

Look up an accessed key through a hash bucket chain. Unlink a hit from its current recency position and move it to the head. On a full-cache miss, remove the tail from both the linked list and its hash bucket, then reuse its node.

Open the 3D visualization

LRU cache with hashing and linked list example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use LRU cache with hashing and linked list

Teaching cache replacement, collision handling and constant-time linked-list relinking.

Choosing an alternative

FIFO evicts by insertion age; LRU changes recency on every hit. A list-only implementation needs a full scan, while hashing supplies expected constant lookup.

Worked example: input and state changes

Example input

1,8,15,1,22,8,29,1 | 3

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Empty cache; recency list runs from MRU at left to LRU at right.

    State at this checkpoint
    {
      "values": [
        "empty",
        "empty",
        "empty"
      ],
      "active": [],
      "order": [],
      "hits": 0,
      "evictions": 0
    }
  2. Step 4: Hit 1: unlink it before moving it to the MRU head.

    State at this checkpoint
    {
      "values": [
        15,
        8,
        1
      ],
      "active": [],
      "order": [
        15,
        8,
        1
      ],
      "hits": 1,
      "evictions": 0
    }
  3. Step 7: Access 22: move to MRU head. Hits 1, evictions 1.

    State at this checkpoint
    {
      "values": [
        22,
        1,
        15
      ],
      "active": [
        0
      ],
      "order": [
        22,
        1,
        15
      ],
      "hits": 1,
      "evictions": 1
    }
  4. Step 11: Access 29: move to MRU head. Hits 1, evictions 3.

    State at this checkpoint
    {
      "values": [
        29,
        8,
        22
      ],
      "active": [
        0
      ],
      "order": [
        29,
        8,
        22
      ],
      "hits": 1,
      "evictions": 3
    }
  5. Step 14: Cache complete: 1 hits, 4 evictions.

    State at this checkpoint
    {
      "values": [
        1,
        29,
        8
      ],
      "active": [],
      "order": [
        1,
        29,
        8
      ],
      "result": 1,
      "hits": 1,
      "evictions": 4
    }

Expected final state

Cache complete: 1 hits, 4 evictions.

Complexity and input bounds

Family: Data structures. Time complexity: O(capacity) worst-case lookup.

Implementation detail: The view uses one to four resident entries, seven chained hash buckets and sixteen accesses. Leftmost is MRU, rightmost LRU. Downloads serialize recency order, bucket heads, hits and evictions. Worst-case lookup scans a collision chain; doubly linked relinking is constant time.

Space complexity: O(capacity + hash buckets) auxiliary storage, plus saved animation frames.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

1..16 access keys (0..99) | cache capacity (1..4). Leftmost displayed entry is most recently used.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

A hit must refresh recency. Eviction must also remove the hash-chain entry. Capacity-one caches must update both head and tail without leaving stale links.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// LRU cache accesses: n 0..16 keys, keys 0..99, capacity 1..4. Hash lookup uses seven separate-chaining buckets; recency uses an actual doubly linked list. Hits move a node to the head; full misses evict the tail and reuse its slot. Returns hit count. Output capacity at least 16: [0] resident count; [1] hits; [2..5] MRU-to-LRU keys (-1 absent); [8..14] hash bucket head keys (-1 absent); [15] evictions. Expected constant lookup with hashing, O(capacity) worst-case collision scan, O(1) recency relinking; O(capacity + buckets) auxiliary space. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, capacity, output) {
    let key = Array(256).fill(0); let previous = Array(256).fill(0); let next = Array(256).fill(0); let hashNext = Array(256).fill(0); let buckets = Array(256).fill(0);
    let head = 0; let tail = 0; let used = 0; let hits = 0; let evictions = 0; let value = 0; let u = 0; let v = 0; let p = 0; let length = 0;
    for (let i = 0; i < n; i++) {
        value = a[i]; u = buckets[value % 7];
        while (u != 0 && key[u] != value) { u = hashNext[u]; }
        if (u != 0) {
            hits++;
            if (previous[u] != 0) { next[previous[u]] = next[u]; } else { head = next[u]; }
            if (next[u] != 0) { previous[next[u]] = previous[u]; } else { tail = previous[u]; }
        } else {
            if (used < capacity) { u = ++used; }
            else {
                u = tail; v = buckets[key[u] % 7]; p = 0;
                while (v != u) { p = v; v = hashNext[v]; }
                if (p != 0) { hashNext[p] = hashNext[u]; } else { buckets[key[u] % 7] = hashNext[u]; }
                tail = previous[u]; if (tail != 0) { next[tail] = 0; } else { head = 0; }
                evictions++;
            }
            key[u] = value; hashNext[u] = buckets[value % 7]; buckets[value % 7] = u;
        }
        previous[u] = 0; next[u] = head;
        if (head != 0) { previous[head] = u; } else { tail = u; }
        head = u;
    }
    for (let i = 0; i < 16; i++) { output[i] = 0; }
    for (let i = 0; i < 4; i++) { output[2 + i] = -1; }
    u = head; while (u != 0) { output[2 + length++] = key[u]; u = next[u]; }
    output[0] = used; output[1] = hits; output[15] = evictions;
    for (let i = 0; i < 7; i++) { output[8 + i] = -1; if (buckets[i] != 0) { output[8 + i] = key[buckets[i]]; } }
    return hits;
}

Source

Primary research on storage hierarchy evaluation and stack replacement policies. The playground implements a bounded hash-plus-linked-list LRU cache, not the paper’s hierarchy-evaluation program.