Least Frequently Used Cache for Hot Reel Metadata
Last updated: November 29, 2026
Quick Overview
Design a fixed-capacity cache with get and put in O(1) that evicts the key with the lowest access count, breaking ties by evicting the least recently used among them.
Meta
November 29, 20260
0
0 solved
Design a fixed-capacity cache with get and put in O(1) that evicts the key with the lowest access count, breaking ties by evicting the least recently used among them.
Reels metadata has a long tail where a few clips stay hot for hours, so frequency matters more than pure recency. Meta interviewers use LFU as a harder sibling of LRU to see whether you can maintain frequency buckets and a min-frequency pointer without any O(n) scans.
What the Interviewer Expects
- Explain why a heap gives O(log n) and is not enough
- Group keys into frequency buckets, each ordered by recency
- Maintain min_freq and update it on touch and insert
- Treat put on an existing key as an access
- Handle capacity zero and eviction ties correctly
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 age frequencies so yesterday's viral reel does not stay forever?
- How would you make this thread safe with minimal contention?
- How would you implement buckets without OrderedDict, using your own linked lists?
- When would LFU be worse than LRU for a feed cache?
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 the value or -1 and counts as a use.put(key, value)inserts or updates; an update also counts as a use.- On overflow, evict the lowest-count key; among ties, the least recen...
High-Level Design
Keep three maps: key to value, key to count, and count to an OrderedDict of keys in recency order. min_freq tracks the smallest count present. Touching a key moves it from bucket f to bucket `f ...