Skip to main content
PreparationZone Logo
PrepZone

Design an LRU Cache

O(1) get/put with HashMap plus doubly-linked list.

Pass checkpoint below

Why this matters

  • LRU cache is a data-structure favourite — tests HashMap + linked list coordination.
  • Appears in rate limiting, session stores, and CDN edge discussions.
  • Thread-safe variant separates staff-level candidates from senior.
HashMap
key → Node
Doubly-linked list
head (MRU)
...
tail (LRU)
Map gives O(1) lookup; list gives O(1) eviction order.

Requirements

Functional

  • get(key) returns value or absent
  • put(key, value) inserts or updates; evicts least-recently-used when at capacity
  • Both operations must be O(1) average time

Non-functional

  • Optional thread-safe variant for concurrent readers/writers
  • Capacity fixed at construction

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Data structures
  • Thread safety
  • CampusOS extension

Test yourself

Answer these before moving on — recall is what makes it stick.