Track Max and Min Frequency Query Strings in Constant Time

Last updated: November 29, 2026

Quick Overview

Design a structure over strings supporting inc(key), dec(key) (removing the key at count 0), getMaxKey() and getMinKey(), each in O(1) time, for tracking hot and cold queries in a cache warmer.

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

0

0

0 solved


Design a structure over strings supporting inc(key), dec(key) (removing the key at count 0), getMaxKey() and getMinKey(), each in O(1) time, for tracking hot and cold queries in a cache warmer.

A Google onsite data structure design problem where a heap would give O(log n) and the interviewer explicitly wants O(1). The intended solution is a doubly linked list of count buckets plus a hash map, and the signal is in getting bucket creation and removal exactly right.

What the Interviewer Expects
  • Keep a doubly linked list of buckets sorted by count, each holding a set of keys
  • Map each key to its current bucket
  • On inc, move the key to the next bucket, creating count + 1 if absent
  • On dec, move to the previous bucket or delete the key at 0
  • Remove empty buckets so max and min are at the list ends
Key Topics to Cover
doubly linked list
hash map
bucket list
O(1) design
LFU-style structure
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
  • Add getKeysWithCount(c) in O(1) plus output size.
  • Support inc by an arbitrary amount.
  • Make it thread safe with minimal contention.
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
  • inc(key): count += 1 (new keys start at 1).
  • dec(key): count -= 1; remove key at 0; key is assumed present.
  • getMaxKey(), getMinKey(): any key with max or min count, or "" if empty.
  • All ...
High-Level Design

Counts change by exactly one, so a key only ever moves to an adjacent bucket. Maintain buckets as a doubly linked list between head and tail sentinels, in increasing count order, each bucket holding a...


Submit Your Answer
Markdown supported