System & Data Structures

LRU Cache

mediumLeetCodeJS / TS6 tests

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
JavaScript & TypeScript starter code included
Run this as a live interview

Practice solo

6 test cases. No second person needed.

Get new interview exercises

Practice problems and review drills, sent when we publish new ones. No spam.

Unsubscribe anytime. We never share your address.