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 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 times

The 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
📚 Book Catalog"Harry Potter" → 3"Sherlock" → 2"Gatsby" → 1📊 Inventory Index1 copy → 1 book2 copies → 1 book3 copies → 1 bookSYNC❓ Query: "Any books with exactly 2 copies?"O(1)LOOKUPCheck Inventory Index: "2 copies → 1 book" ✅TRUE
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.
Asked in
Google 42 Amazon 38 Meta 28 Microsoft 35
35.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen