Design an LFU Cache for Hot Pin Metadata Lookups
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity cache for Pin metadata with O(1) get and put that evicts the least frequently used key, breaking frequency ties by evicting the least recently used among them.
0
0
0 solved
Implement a fixed-capacity cache for Pin metadata with O(1) get and put that evicts the least frequently used key, breaking frequency ties by evicting the least recently used among them.
Pinterest serves enormous read traffic on Pin metadata where a small set of evergreen Pins stays hot for years, which makes frequency-aware eviction attractive compared with pure LRU. The onsite interviewer wants O(1) operations, a correct min-frequency pointer, and a discussion of LFU's weaknesses such as stale popularity.
What the Interviewer Expects
- Keeps a frequency map and per-frequency buckets ordered by recency
- Maintains min_freq correctly on touch and on insert
- Achieves O(1) for both operations
- Handles capacity 0 and updates to existing keys
- Discusses frequency aging so yesterday's viral Pin does not stay forever
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you add decay so old popularity fades?
- How does this compare to TinyLFU admission with an LRU main cache?
- How would you make it thread-safe without one global lock?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- get(key) returns value or None and counts as a use.
- put(key, value) inserts or updates (an update also counts as a use).
- On insert at capacity, evict the lowest-frequency key; among ties, the le...
High-Level Design
Three maps: key to value, key to frequency, and frequency to an OrderedDict of keys in recency order. Touching a key moves it from bucket f to bucket f + 1 (appended at the most recent end). If bucket...