146. LRU Cache
mediumAsked at SnapSnap Memories and story previews rely on an LRU layer to decide which media blobs to keep in RAM — this problem is the exact data-structure contract that cache layer implements.
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 capacity, get(key) returning -1 if missing, and put(key, value) which evicts the least recently used key when at capacity. Both operations must run in O(1) average time.
Constraints
1 <= capacity <= 30000 <= key <= 10^40 <= value <= 10^5At most 2 * 10^5 calls will be made to get and put
Examples
Example 1
LRUCache(2); put(1,1); put(2,2); get(1) → 1; put(3,3); get(2) → -1; put(4,4); get(1) → -1; get(3) → 3; get(4) → 4[1,-1,-1,3,4]Example 2
LRUCache(1); put(2,1); get(2) → 1; put(3,2); get(2) → -1; get(3) → 2[1,-1,2]Approaches
1. Ordered Map (suboptimal)
Use a Map (insertion-ordered in JS) and re-insert on every get/put to track recency. Delete + re-insert is O(1) amortized in V8 but the approach is conceptually clear as a stepping stone.
- 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. HashMap + Doubly Linked List
HashMap gives O(1) lookup; doubly linked list (with dummy head/tail sentinels) gives O(1) move-to-front and O(1) evict-from-tail. This is the canonical interview answer Snap expects at L4+.
- 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:
Snap-specific tips
Snap's infrastructure team almost always follows up with 'how would you make this thread-safe?' and 'what eviction policy would you swap in for Snap Stories where recency matters less than access frequency?' Have answers for both.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill LRU Cache and other Snap interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →