The Forgetful Machine
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L5
Problem
A fixed-size in-memory cache holds at most `capacity` key-value entries: `get(key)` returns the stored value or -1 when the key is absent, and `put(key, value)` adds or overwrites an entry, dropping the entry that has gone longest without a read or write whenever an insertion would push the cache past `capacity`. Implement that cache, then implement `LRUCache_driver(capacity, ops)`, which builds one cache of the given capacity, replays each operation in `ops` (either `['get', k]` or `['put', k, v]`), and returns the values produced by the `get` operations in the order they ran.
Summary
It remembers everything, until it does not.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.