← 返回 bloomberg 的题目列表Refactor an LRU Cache into a Pluggable Eviction-Policy Cache
类型:online_judge
Problem: Refactor an LRU Cache into a Pluggable Eviction-Policy Cache
You are given a local in-memory cache implementation that only supports LRU (Least Recently Used) eviction. Refactor the design so that storage concerns are decoupled from eviction-policy concerns, allowing the cache to support multiple policies without changing the cache core.
Implement:
A generic Cache with get(key), put(key, value), and delete(key).
An eviction-policy abstraction that the cache can use without knowing policy-specific internals.
LRUPolicy and LFUPolicy implementations.
A design that can support future policies, such as FIFO or MRU, without modifying Cache.
The cache has a fixed positive capacity. get hits and updates to an existing key count as accesses. When full, inserting a new key evicts a key selected by the configured policy. For LFU, break equal-frequency ties using LRU.
Input starts with LRU capacity or LFU capacity, followed by put key value, get key, and delete key operations. Print the result of every get, or None for a miss.
There are at most 2 * 10^5 operations. Target amortized O(1) time for each operation.
Example
Input
LRU 2
put 1 1
put 2 2
get 1
put 3 3
get 2
get 3
Output
1
None
3