Frequency Tracker - Problem
Design a dynamic data structure that efficiently tracks element frequencies and answers queries about frequency counts in real-time. You need to implement a
•
•
•
The challenge is maintaining O(1) average time complexity for all operations while keeping track of both number counts and frequency counts simultaneously.
Example:
FrequencyTracker class that can:•
add(number) - Insert a number into the data structure•
deleteOne(number) - Remove exactly one occurrence of a number (if it exists)•
hasFrequency(frequency) - Check if any number appears exactly frequency timesThe challenge is maintaining O(1) average time complexity for all operations while keeping track of both number counts and frequency counts simultaneously.
Example:
tracker = FrequencyTracker()tracker.add(3) → adds 3 (freq: 1)tracker.add(3) → adds 3 again (freq: 2)tracker.hasFrequency(2) → returns True (3 appears 2 times)tracker.deleteOne(3) → removes one 3 (freq: 1)tracker.hasFrequency(2) → returns False (no number appears 2 times) Input & Output
example_1.py — Basic Operations
$
Input:
["FrequencyTracker", "add", "add", "hasFrequency", "add", "hasFrequency", "deleteOne", "hasFrequency"]
[[], [3], [3], [2], [1], [1], [3], [2]]
›
Output:
[null, null, null, true, null, true, null, false]
💡 Note:
Initially empty. Add 3 twice (freq=2), hasFrequency(2)=true. Add 1 (freq=1), hasFrequency(1)=true. Delete one 3 (freq=1), now no number has frequency 2, so hasFrequency(2)=false.
example_2.py — Multiple Frequencies
$
Input:
["FrequencyTracker", "add", "add", "add", "add", "hasFrequency", "hasFrequency", "hasFrequency"]
[[], [1], [2], [1], [2], [1], [2], [3]]
›
Output:
[null, null, null, null, null, false, true, false]
💡 Note:
Add 1,2,1,2 → both 1 and 2 have frequency 2. hasFrequency(1)=false (no number appears once), hasFrequency(2)=true (both numbers appear twice), hasFrequency(3)=false (no number appears 3 times).
example_3.py — Edge Cases
$
Input:
["FrequencyTracker", "deleteOne", "hasFrequency", "add", "deleteOne", "hasFrequency"]
[[], [1], [1], [5], [5], [0]]
›
Output:
[null, null, false, null, null, false]
💡 Note:
Delete from empty tracker (no effect). hasFrequency(1)=false (empty). Add 5, delete 5 (back to empty). hasFrequency(0)=false (frequency 0 is invalid).
Constraints
- 1 ≤ number ≤ 105
- 1 ≤ frequency ≤ 105
- At most 2 × 105 calls will be made to add, deleteOne, and hasFrequency
- The data structure should handle real-time operations efficiently
Visualization
Tap to expand
Understanding the Visualization
1
Book Catalog
Maintain a catalog of each book and how many copies you have
2
Inventory Index
Keep an index of which copy counts exist (e.g., '5 books have 3 copies each')
3
Synchronized Updates
When books are added/removed, update both the catalog and inventory index simultaneously
4
Instant Queries
Answer 'Any books with exactly N copies?' by checking the inventory index in O(1) time
Key Takeaway
🎯 Key Insight: By maintaining two synchronized hash tables (book catalog + inventory index), we can answer frequency queries instantly while keeping updates efficient. This dual-mapping approach is the cornerstone of O(1) frequency tracking.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code