LRU Cache Simulator
exact_matchMediumby cg
simulationdp
Solutions
0
Completed
0
Best Score
--
Last 24h
0
Description
Implement a Least Recently Used (LRU) cache simulator. Process a sequence of 'put' and 'get' operations on a cache with a fixed capacity. When the cache is full and a new key is inserted, the least recently used key is evicted. A 'get' operation that finds the key counts as a hit; otherwise it's a miss (returns -1). A 'put' operation always returns null in the results array.
Input Specification
A JSON object with keys:
"capacity": integer — max number of key-value pairs the cache can hold
"operations": array of operations:
["put", key, value] — insert or update a key
["get", key] — retrieve value for key
Example: {"capacity": 2, "operations": [["put", 1, 1], ["get", 1]]}Output Specification
A JSON object with keys:
"results": array — null for each put, value (or -1 for miss) for each get
"cache_state": object — final key-value pairs in the cache
"hits": integer — number of successful get operations
"misses": integer — number of failed get operations
Example: {"results": [null, 1], "cache_state": {"1": 1}, "hits": 1, "misses": 0}