LFU Cache
LFU Cache is a hard coding interview question covering design, hash maps — the kind asked at FAANG (Facebook/Meta, Apple, Amazon, Netflix, Google) and AI-first companies like Anthropic, OpenAI, and Nvidia.
LFU Cache — Problem Statement
Design a Least Frequently Used (LFU) cache with O(1) get(key) and put(key, value). Evict the least frequently used key. On frequency tie, evict the least recently used.
Example (capacity=2): put(1,1), put(2,2), get(1) -> 1 put(3,3) -- evicts key 2 (freq 1 < freq 2 for key 1) get(2) -> -1, get(3) -> 3, get(1) -> 1
Solution walkthrough on Pro
Upgrade to Pro to unlock the guided hints, the step-by-step walkthrough, solutions in Python, C++, Java, and Go, and complexity analysis.
What's inside this guide
- A three-step hint ladder before the solution
- A 6-step trace of the code on an example
- Solutions in 4 languages: Python, C++, Java and Go
- Time and space complexity, stated up front
- A written model answer
Not ready to upgrade? Practise these instead
Fully unlocked, no account needed — solutions, walkthroughs and follow-up answers included.
Or start the Study Plan — a week-by-week order for your loop, and week 1 is entirely free.