Back to Problem Library
System & Data Structures

LRU Cache

mediumJS / TS

Problem Statement

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the `LRUCache` class: - `LRUCache(capacity)` Initialize the LRU cache with positive size capacity. - `get(key)` Return the value of key if it exists, otherwise return -1. - `put(key, value)` Update or insert the value. When capacity is reached, invalidate the least recently used item.

Examples

Example:
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
cache.get(1);    // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2);    // returns -1
Includes TypeScript & JavaScript starter code
Start Interview with this Problem