Data Structures19 sections · 729 units
Open in Course

Problem - LRU Cache

Hash map + doubly linked list

Design a data structure that implements a Least Recently Used (LRU) cache. Implement:

  • get(key): Return the value if key exists, else 1-1. Mark as recently used.
  • put(key, value): Insert or update. If at capacity, evict the least recently used item first.

Both operations must be O(1)O(1).

You're solving a classic system design question that combines two data structures. The hash map provides O(1)O(1) lookup.

The doubly linked list maintains usage order with O(1)O(1) move-to-front and remove-from-back operations. Constraints: capacity from 11 to 30003000, up to 21052 \cdot 10^5 operations.