Overview
Strict O(1) LRU and Shah-Mitra-Matani frequency-bucketed LFU caches built over sentinel-guarded intrusive doubly linked lists with zero heap-log overhead.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Sentinel-Guarded Intrusive Doubly Linked List
- Core Mechanical Concept: Eliminating Null Checks with Dummy Head and Tail Sentinels
- Target Module:
index.js(DoublyLinkedList.addFront / DoublyLinkedList.remove) - Engineering Specification: Implement
addFront(node)andremove(node)onDoublyLinkedList. Becausethis.headandthis.tailare permanent dummy sentinel nodes, every real node always has non-nullnode.prevandnode.next.addFrontsplicesnodebetweenheadandhead.next;removelinksnode.prev.next = node.nextandnode.next.prev = node.prev. - Verification Invariant: splices and unlinks nodes in O(1) using sentinel head and tail
Step 2: O(1) LRU Cache via Hash Map + Recency List
- Core Mechanical Concept: Composing Hash Lookups with Intrusive List Promotion
- Target Module:
index.js(LRUCache.get / LRUCache.put) - Engineering Specification: Implement
get(key)andput(key, value)onLRUCache. - Most recently used node sits atlist.head.next; least recently used sits atlist.tail.prev. - Onget(key): unlink node andaddFront(node). - Onput(key, value): ifmap.size === capacity, evictlist.popBack()and delete its key frommap. - Verification Invariant: evicts least-recently-used key in O(1) time
Step 3: Frequency Bucket Promotion & minFreq Invariant
- Core Mechanical Concept: Shah-Mitra-Matani O(1) Frequency Bucket Advancement
- Target Module:
index.js(LFUCache._promote) - Engineering Specification: Implement
_promote(node)onLFUCache. Whennodemoves fromoldFreqtooldFreq + 1, remove it frombuckets.get(oldFreq). If that bucket becomes empty andthis.minFreq === oldFreq, incrementthis.minFreq = oldFreq + 1. Then insertnodeat the front ofbuckets.get(oldFreq + 1). - Verification Invariant: advances minFreq exactly when the minimum frequency bucket empties
Step 4: LFU Insertion & Tie-Breaking LRU Eviction
- Core Mechanical Concept: Evicting Tail of buckets[minFreq] & Resetting minFreq = 1
- Target Module:
index.js(LFUCache.put) - Engineering Specification: Implement
put(key, value)onLFUCache. 1. Ifkeyexists, updatenode.value = valueand callthis._promote(node). 2. If at capacity, pop the LRU victim fromthis.buckets.get(this.minFreq).popBack()and delete it fromthis.nodes. 3. Insert the new node into bucket1and setthis.minFreq = 1. - Verification Invariant: evicts lowest-frequency key and breaks ties by least-recently-used order
Asymptotic & Memory Layout Guarantees
- Category: Linked Structures & Caching
- Difficulty: Advanced (~35 minutes)
- Key Architectural Highlights:
• Sentinel-guarded doubly linked list eliminating null-pointer branches
• Shah-Mitra-Matani O(1) LFU cache with frequency-bucketed LRU tie-breaking
• Exact minFreq invariant maintenance across hits, promotions, and evictions - Tags & Primitives:
LFU Cache,LRU Cache,Intrusive Linked List,O(1) Eviction,Frequency Buckets
Production Systems Context & Verification
This from-scratch implementation is engineered with zero external dependencies and deterministic verification harnesses. Every internal invariant—from pointer restoration and bitmask addressing to amortized potential bounds and tree height balance—is asserted directly in test.js. Use the interactive browser workbench above to complete the guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.