LFU Cache for Product Detail Page Lookups

Last updated: November 29, 2026

Quick Overview

Design a fixed capacity cache with O(1) get and put that evicts the least frequently used key, breaking frequency ties by evicting the least recently used among them.

Flipkart
Coding & Algorithms
Software Engineer
Flipkart
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Design a fixed capacity cache with O(1) get and put that evicts the least frequently used key, breaking frequency ties by evicting the least recently used among them.

A Flipkart machine coding style question tied to caching product detail data during Big Billion Days, where a few listings are extremely hot. The interviewer checks whether you can get true O(1) operations with frequency buckets and a tracked minimum frequency.

What the Interviewer Expects
  • Explain why a heap gives O(log n), not O(1)
  • Keep key to (value, freq) plus freq to ordered set of keys
  • Track min_freq and reset it to 1 on insert
  • Break ties by recency within a frequency bucket
  • Handle capacity 0 and updating an existing key
Key Topics to Cover
design a data structure
LFU cache
hash map
ordered dict
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 add frequency decay so yesterday's hot items can be evicted?
  • How does this compare with LRU for a flash sale traffic pattern?
  • How would you make it thread safe without a global lock?
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 (update also counts as a use).
  • On overflow, evict the key with the lowest use count; among ties, the ...
High-Level Design

A heap keyed by frequency is O(log n) per operation, so instead use three maps:

  • vals: key to value.
  • freq: key to count.
  • buckets: count to an OrderedDict of keys in recency order (oldest ...

Submit Your Answer
Markdown supported