Frequency Tracker - Problem
Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.
Implement the FrequencyTracker class:
FrequencyTracker(): Initializes the FrequencyTracker object with an empty array initially.void add(int number): Adds number to the data structure.void deleteOne(int number): Deletes one occurrence of number from the data structure. The data structure may not contain number, and in this case nothing is deleted.bool hasFrequency(int frequency): Returns true if there is a number in the data structure that occurs frequency number of times, otherwise, it returns false.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = ["FrequencyTracker", "add", "add", "hasFrequency"], values = [0, 3, 3, 2]
›
Output:
[null, null, null, true]
💡 Note:
Create tracker, add 3 twice (frequency 2), hasFrequency(2) returns true since 3 appears 2 times
Example 2 — Delete Operations
$
Input:
operations = ["FrequencyTracker", "add", "deleteOne", "hasFrequency"], values = [0, 1, 1, 1]
›
Output:
[null, null, null, false]
💡 Note:
Add 1, delete 1, now no numbers exist so hasFrequency(1) returns false
Example 3 — Multiple Numbers
$
Input:
operations = ["FrequencyTracker", "add", "add", "add", "hasFrequency", "hasFrequency"], values = [0, 1, 1, 2, 2, 1]
›
Output:
[null, null, null, null, true, true]
💡 Note:
Add 1,1,2. Number 1 has frequency 2, number 2 has frequency 1. Both queries return true
Constraints
- 1 ≤ number ≤ 105
- 1 ≤ frequency ≤ 105
- At most 2 × 105 calls to add, deleteOne, and hasFrequency
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sequence of add/delete/query operations
2
Process
Maintain frequency counts efficiently
3
Output
Return boolean results for hasFrequency queries
Key Takeaway
🎯 Key Insight: Using two hash maps allows instant frequency lookups while maintaining counts efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code