The Eviction Policy
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
Problem
A read-through cache sits in front of a slow store and holds at most a fixed number of entries; once it is full, the entry that has gone longest without being touched is dropped to make room for a new one. Replay `operations` against such a cache and return one result per operation, in order. `operations[0]` is always `['LRUCache', capacity]`, which sets the entry limit (`capacity` is at least 1); each later operation is either `['put', key, value]`, which inserts or overwrites a key, or `['get', key]`, which returns that key's value or -1 when the key is absent. Both reading and writing a key count as touching it, so the next eviction removes whatever key has stayed idle longest. The constructor and every `put` contribute `None` to the result list; each `get` contributes the value it found, or -1.
Summary
Fixed capacity. The key left idle longest is the one that goes.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.