Design an LRU Cache Object Model
Model a bounded key/value cache with exact access-based eviction, focused contracts and a capacity-resize follow-up.
Design the object-oriented core of a least-recently-used (LRU) cache. LRU means that, when a new key arrives at a full cache, the resident whose latest successful access is oldest is evicted.
For this exercise, assume one in-memory cache in a single-threaded process, immutable string keys and values including empty strings, and a positive capacity measured in entries. get(key) returns the value or an explicit missing result.
A successful get or update of an existing key makes that key most recent; a missing read changes nothing. put(key,value) replaces a resident value without adding an entry, or inserts a new key after evicting exactly one least-recent resident when full.
Reject nonpositive construction capacity before any usable cache exists. Keep resident count at or below capacity. Aim for expected constant-time bookkeeping for get and put and space proportional to capacity; choose and justify the responsibilities and representation yourself.
Explain requirements, object ownership, interfaces and operation interactions; implementation code is not required.
For the follow-up, assume resize(newCapacity) accepts positive capacities, shrinks by repeatedly evicting least-recent residents, grows without changing existing access order, and rejects invalid sizes without changing any state.
Expiration, simultaneous callers, selectable eviction policies, loading, persistence and distribution are outside this exercise.