This question evaluates a candidate's ability to design and implement efficient cache eviction policies while managing time and space complexity guarantees for mutable key-value stores.
Design and implement an LRU (Least Recently Used) cache that supports the following operations in average O(1) time:
get(key) -> value
: Return the value associated with
key
if it exists in the cache; otherwise return
-1
. Accessing a key makes it
most recently used
.
put(key, value)
: Insert or update the value for
key
. If inserting causes the number of keys to exceed the cache
capacity
, evict the
least recently used
key.
You will implement a class (or module) with:
LRUCache(capacity)
get(key)
and
put(key, value)
1 <= capacity <= 10^5
get
or updated via
put
.