System Design Quiz Practice
Active Recall Drill Session
← Exit SessionQuestion 1 of 1
hardSystem Design
Given this LRUCache with capacity 2, what does the final sequence of gets return?
code
class LRUCache {
constructor(cap) { this.capacity = cap; this.cache = new Map(); }
get(key) {
if (!this.cache.has(key)) return undefined;
const v = this.cache.get(key);
this.cache.delete(key); this.cache.set(key, v);
return v;
}
set(key, value) {
if (this.cache.has(key)) this.cache.delete(key);
else if (this.cache.size >= this.capacity) {
this.cache.delete(this.cache.keys().next().value);
}
this.cache.set(key, value);
}
}
const c = new LRUCache(2);
c.set("a", 1);
c.set("b", 2);
c.get("a");
c.set("c", 3);
console.log(c.get("b"), c.get("a"), c.get("c"));