Implement LRU, Extend to LFU, Analyze Complexity
IntuitSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of cache eviction policies (LRU and LFU), frequency tracking, and the design of data structures that support efficient get/put operations while meeting time and space complexity constraints.
Implement an in-memory Least Recently Used (LRU) cache with capacity N that supports get(key) -> value and put(key, value). Achieve O(
- average time per operation using appropriate data structures; justify your choices and analyze time and space complexity. Follow-up: Modify the design to implement a Least Frequently Used (LFU) cache with O(
- or amortized O(
- operations; explain the eviction policy, how you track and update frequencies on get/put, and how you break ties by recency.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.