The Frequency Tracker interview question asks you to design a data structure that can keep track of the frequencies of a stream of numbers. You need to support three operations efficiently:
add(number): Add a number to the data structure.deleteOne(number): Remove one occurrence of a number (if it exists).hasFrequency(frequency): Return true if there is any number in the data structure that currently has the exact given frequency.Microsoft uses this Design coding problem to test your ability to manage state across multiple interconnected Hash Tables. While tracking frequencies is easy (using a simple map), checking if a specific frequency exists in time requires a "reverse lookup" table. It evaluates whether you can keep two data structures synchronized during additions and deletions without introducing bugs or performance bottlenecks.
This problem relies on a Double Hash Table (or Array) Pattern.
counts): Tracks the frequency of each number (number -> frequency).freqCounts): Tracks how many numbers currently have a specific frequency (frequency -> count_of_numbers).
When you add or deleteOne, you must update counts first, and then update freqCounts by decrementing the count for the old frequency and incrementing the count for the new frequency.add(3): counts[3] becomes 1. freqCounts[1] becomes 1.add(3): counts[3] becomes 2. freqCounts[1] becomes 0. freqCounts[2] becomes 1.hasFrequency(2): Looks up freqCounts[2]. It is > 0. Returns True.deleteOne(3): counts[3] becomes 1. freqCounts[2] becomes 0. freqCounts[1] becomes 1.hasFrequency(2): Looks up freqCounts[2]. It is 0. Returns False.counts map to answer hasFrequency, which is too slow for large streams.freqCounts when a number's count changes.counts[number] below zero when deleteOne is called on a number that isn't in the structure.Whenever a problem asks for lookups based on a property that changes (like frequency), you almost always need a secondary Hash Table to index that property directly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design A Leaderboard | Medium | Solve | |
| Finding Pairs With a Certain Sum | Medium | Solve | |
| Apply Discount Every n Orders | Medium | Solve | |
| Design Underground System | Medium | Solve | |
| Logger Rate Limiter | Easy | Solve |