Design a frequency tracker with constant-time add, delete-one and has-frequency operations
Company: Nuro
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a frequency tracker data structure that maintains a collection of integers and supports efficient queries about how often values appear in it.
The structure should allow adding numbers, deleting one occurrence of a number, and checking whether any number currently has a given frequency.
Implement the following methods:
- `add(number: int) -> None` adds the given number to the data structure.
- `deleteOne(number: int) -> None` deletes one occurrence of the given number. It does nothing if the number is not present.
- `hasFrequency(frequency: int) -> bool` returns `True` if any number in the data structure currently has the given frequency, otherwise `False`.
Example 1:
```text
Input:
["FrequencyTracker", "add", "add", "hasFrequency"]
[[], [3], [3], [2]]
Output: [null, null, null, true]
Explanation:
tracker = FrequencyTracker() # empty tracker
tracker.add(3) # counts: {3:1}, freqs: {1:1}
tracker.add(3) # counts: {3:2}, freqs: {2:1}
tracker.hasFrequency(2) # -> True (number 3 appears twice)
```
Example 2:
```text
Input:
["FrequencyTracker", "add", "deleteOne", "hasFrequency"]
[[], [1], [1], [1]]
Output: [null, null, null, false]
Explanation:
tracker = FrequencyTracker() # empty tracker
tracker.add(1) # counts: {1:1}, freqs: {1:1}
tracker.deleteOne(1) # counts: {}, freqs: {}
tracker.hasFrequency(1) # -> False (no number has frequency 1)
```
```hint Count the counts
Per-number counts alone force `hasFrequency` to scan every stored number. Think about a second quantity, keyed by frequency, that you can update whenever one number's count changes.
```
```hint Watch the transitions
Each `add` or `deleteOne` moves one number from one frequency to an adjacent one. Check what happens when a count drops from 1 to 0, and when the number being deleted is not present at all.
```
### Constraints & Assumptions
- `1 <= number <= 10^5`
- `1 <= frequency <= 10^5`
- At most `10^5` total calls are made to `add`, `deleteOne` and `hasFrequency`.
### What a Strong Answer Covers
- How `hasFrequency` avoids scanning every stored number.
- How the per-number counts and any frequency-level bookkeeping stay consistent on every `add` and `deleteOne`.
- Handling of a delete for a number that is not present, and of a count that drops to zero.
- What happens when several numbers share the same frequency and only one of them changes.
- Time and space complexity of each operation.
### Follow-up Questions
- How would you add a method that returns every number currently at a given frequency?
- How would you support `maxFrequency()`, the highest count of any stored number, in O(1) time?
- How would you make the tracker safe to call from several threads at once?
Overview: Design a frequency tracker that supports adding numbers, deleting one occurrence, and checking whether any number currently appears exactly a given number of times. It tests hash map bookkeeping, keeping two views of the data consistent through every update, and reaching constant time for all three operations.