Your question is LRU Cache for Fast Retrieval. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
At Acme Analytics, repeated lookups for the same records are slowing down application performance. Implement an LRU (Least Recently Used) cache to optimize repeated data retrieval by keeping only the most recently accessed items in memory.
Design a class LRUCache that supports these operations in O(1) average time:
get(key) — return the value for key if it exists, otherwise return -1.put(key, value) — insert or update the key-value pair. If the cache exceeds its capacity, evict the least recently used key.capacity, followed by a sequence of operations: get(key) and put(key, value).get(key), return the stored value or -1 if the key is not present.def run_lru_cache(capacity, operations):