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.

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

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
Hash Map
Ordered Dictionary
Cache Eviction
LFU
Data Structure Design
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 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 Problems
Sample 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] = value and freq[key] = count.
  • buckets[f], an OrderedDict of keys used exactly f times, in recency order (oldest first).
  • min_freq, the smallest f with ...

Submit Your Answer
Markdown supported