The Frequency Eviction
A hard Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- hard
- Seniority
- L5
Problem
You are replaying a captured operation log against an LFU (least-frequently-used) cache to reproduce the value each call returned. The cache holds at most `capacity` entries: `get(key)` returns the stored value or -1 when the key is absent, and `put(key, value)` inserts or updates an entry, where both a successful `get` and an update count as an access toward a key's frequency. When a `put` on a new key would exceed `capacity`, evict the least-frequently-used key first, breaking ties by removing the least-recently-used among them; a cache constructed with `capacity <= 0` stores nothing. `run_lfu_cache(operations, args)` receives parallel lists where `operations[0]` is always "LFUCache" (the constructor) and each `args[i]` is the argument list for `operations[i]`; replay them against one instance and return the result of each call, using `None` for the constructor and for every `put`, and the returned value (or -1) for every `get`.
Summary
When storage is tight, something has to go.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.