Refactor a Cache for Pluggable Eviction Policies
Refactor a working LRU cache into a generic in-memory cache that cleanly supports LRU, LFU, and future policies.
For this exercise, assume a fixed-capacity in-memory string key/value cache currently has LRU behavior embedded in its implementation. Refactor it so one cache implementation supports a construction-time choice of LRU or LFU without changing the caller-facing get and put operations.
Get returns a value or a missing result. Put updates an existing key or inserts a new key; when a new key arrives at capacity, exactly one resident key is evicted before the new key is installed. A successful get or existing-key update refreshes LRU recency and increments LFU frequency.
LRU evicts the least recently accessed entry. LFU evicts the least frequently accessed entry and breaks frequency ties by least recent access. Keep every representation of resident keys and eviction order consistent, but choose and justify the responsibility boundary and internal collaboration contract yourself.
Implement the bounded in-memory refactor and working LRU and LFU behavior. The design is single-threaded, with no persistence, distributed cache behavior, or read-through loading. A follow-up asks how FIFO can be added without changing the cache public API or duplicating the core get and put flow.