Problem, as an interviewer would state it:
"Design an LRU cache with get(key) and put(key, value) in O(1). Evict the least recently used entry when capacity is exceeded. Use a Map or a doubly linked list."
Examples:
const c = new LRUCache(2);
c.put(1, 1); c.put(2, 2);
c.get(1); // 1
c.put(3, 3); // evicts key 2
c.get(2); // not found
Clarifying questions expected:
- Does a
getcount as a "use" that refreshes recency, and doesputon an existing key too? - What should
getreturn for a missing key? - What if
capacityis zero or invalid?
Code / implementation expected: Yes — real, direct proof of the eviction order, recency refresh on both operations, and the O(1) scaling.