I started with a basic map and a list of keys, then realized the eviction callback needs visibility into cache internals to make any real decision.
Start by clarifying requirements and defining the cache interface with a pluggable eviction policy. Then design the core cache as a composition of a storage map and an eviction policy object, ensuring the policy is notified on every access and insertion. Finally, discuss trade-offs like thread safety, policy state management, and performance overhead.
Pro tip: Emphasize that the eviction policy should be an interface with methods like `onGet`, `onPut`, and `evict`, and that the cache should delegate decisions to it. This shows you understand the Strategy pattern and separation of concerns.
Ask about expected cache size, concurrency needs, and whether policies need to be swapped at runtime. Confirm that the policy is user-supplied and that the cache should not know about specific policies.
Design an EvictionPolicy interface with methods to track accesses and select a victim. The cache will use a map for O(1) lookups and delegate eviction decisions to the policy.
On get, update the policy's access records and return the value. On put, if the cache is full, call the policy to get a key to evict, remove it, then insert the new item and update the policy.
Show how LRU, LFU, and FIFO can be implemented as separate classes implementing the EvictionPolicy interface, demonstrating the flexibility of the design.
Address thread safety (e.g., using locks or concurrent structures), performance overhead of policy updates, and how to handle custom policies that may need additional state.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.