Least Frequently Used Cache for Map Tiles With Constant-Time Operations
Last updated: November 29, 2026
Quick Overview
Implement an LFU cache with a fixed capacity supporting get(key) and put(key, value) in O(1) average time; on eviction remove the least frequently used key, breaking ties by least recently used.
0
0
0 solved
Implement an LFU cache with a fixed capacity supporting get(key) and put(key, value) in O(1) average time; on eviction remove the least frequently used key, breaking ties by least recently used.
A Google onsite data structure design question, framed around caching Google Maps tiles where popular tiles should stay resident. The interviewer wants a careful composition of hash maps and ordered buckets, and tracks how you maintain the minimum frequency in O(1).
What the Interviewer Expects
- Stores key to (value, frequency)
- Keeps a frequency to ordered-keys map for LRU tie-breaks
- Maintains min_freq and updates it correctly
- Resets min_freq to 1 on a new insert
- Handles capacity 0 and updates to existing keys
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 frequency decay so old popularity fades?
- Compare LFU with LRU and with W-TinyLFU for a tile cache.
- Make it thread-safe for a multi-threaded tile server.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
LFUCache(capacity).get(key): return value or -1; counts as a use.put(key, value): insert or update; an update counts as a use. If inserting into a full cache, evict the LFU key, with LRU...
High-Level Design
Use three structures:
vals[key] = valueandfreq[key] = count.buckets[f], anOrderedDictof keys used exactly f times, in recency order (oldest first).min_freq, the smallest f with ...