Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Thread-Safe LRU Cache

Build a bounded in-memory cache that preserves exact least-recently-used eviction when several threads share it.

Design and implement an LRU cache from scratch, then explain how simultaneous callers preserve its consistency. LRU means least recently used: an insertion into a full cache evicts the entry whose latest successful access is oldest.

For this exercise, assume one cache instance in one process is shared by multiple application threads. Keys and values are immutable strings, including empty strings, and capacity is a fixed positive entry count. get(key) returns a value or an explicit missing result.

A successful get and an update of an existing key make that key most recent; a missing get changes nothing. put(key,value) updates a resident entry without increasing the entry count, or inserts a new entry, evicting exactly one least-recent entry first when full.

Each call must behave as one indivisible operation in a valid serial order that respects calls that finish before another begins; no caller may observe partially updated state. Reject invalid capacity before constructing a usable cache.

Aim for expected constant-time bookkeeping per get or put and space proportional to capacity; waiting for synchronization is separate from that cost. Choose your object responsibilities and synchronization boundary. Provide working bounded code or precise pseudocode for the core and focused tests.

Persistence, expiration, loading from another service, callbacks, distributed replicas and selectable eviction policies are outside this exercise. The final discussion explores concurrent schedules and contention while retaining exact LRU behavior.