Build an LFU Cache for Hot Video Metadata Lookups
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity cache for video metadata where `get` and `put` run in O(1). When full, evict the least frequently used key, breaking ties by evicting the least recently used among them.
TikTok
November 29, 20260
0
0 solved
Implement a fixed-capacity cache for video metadata where `get` and `put` run in O(1). When full, evict the least frequently used key, breaking ties by evicting the least recently used among them.
Video metadata (author, music, captions, region rules) is read on every feed impression, and TikTok onsite rounds use LFU as a data structure design question with a production story attached. The interviewer is checking whether you can keep frequency buckets and recency order in sync with O(1) operations, and whether you know when LFU beats LRU for skewed viral traffic.
What the Interviewer Expects
- Explains why LRU alone fails for viral, heavily skewed traffic
- Maintains count to ordered-keys buckets plus a tracked minimum frequency
- All operations O(1), including the tie-break on recency
- Correctly updates min_freq when a bucket empties
- Handles capacity zero and updating an existing key
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 aging so a video that was hot last week can be evicted?
- How would you make this thread-safe without a global lock becoming a bottleneck?
- How would TinyLFU or a count-min sketch admission policy change the design?
- How would you measure in production whether LFU actually beats LRU here?
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 None and counts as a use. put(key, value) inserts or updates (an update also counts as a use). At capacity, evict the key with the smallest use count; among ties, the...
High-Level Design
Three maps:
vals: key to value.freq: key to its use count.buckets: count to anOrderedDictof keys in recency order (oldest first).
Plus min_freq, the smallest count with a non-emp...