This question evaluates competency in designing efficient key-value caching mechanisms, enforcing capacity constraints and eviction policies while maintaining O(1) average-time operations, testing algorithmic efficiency and stateful data-structure design.
Implement a fixed-capacity key-value cache with least-recently-used eviction.
Support the following operations:
get(key) -> value
: return the value associated with
key
if it exists; otherwise return
-1
put(key, value)
: insert or update the key; if the cache exceeds its capacity, evict the least recently used key
Both operations must run in O(1) average time.
You may assume:
capacity
is a positive integer