This question evaluates understanding of data structures and algorithmic optimization for constant-time cache operations, with emphasis on cache eviction policies and state management.
Design and implement a fixed-capacity Least Recently Used (LRU) cache.
get(key) -> value | -1/null
: Return the value if present; otherwise indicate missing. Accessing a key makes it
most recently used
.
put(key, value)
: Insert or update the value. Updating also makes it
most recently used
.
O(1)
average for both
get
and
put
.
You may choose an interface/class-based implementation (common in interviews). Clearly define how missing keys are represented (e.g., -1 for integers).