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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Hash map
Doubly linked list
Cache eviction
Data structure design
Amortized analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported