Imagine you're building a real-time analytics system that tracks the popularity of different content IDs on a social media platform. As users interact with content throughout the day, IDs get added to or removed from your tracking system, and you need to continuously monitor which content is currently most popular.

You're given two arrays of equal length n:

  • nums: An array where each element represents a content ID
  • freq: An array where each element indicates how many times the corresponding ID should be added (positive values) or removed (negative values) from your tracking system

At each step i, you update your collection based on nums[i] and freq[i], then need to report the count of the most frequent ID currently in your system.

Example: If nums = [2, 3, 2, 1] and freq = [3, 2, -2, 1]:

  • Step 0: Add 3 copies of ID 2 → Collection: {2: 3} → Most frequent count: 3
  • Step 1: Add 2 copies of ID 3 → Collection: {2: 3, 3: 2} → Most frequent count: 3
  • Step 2: Remove 2 copies of ID 2 → Collection: {2: 1, 3: 2} → Most frequent count: 2
  • Step 3: Add 1 copy of ID 1 → Collection: {2: 1, 3: 2, 1: 1} → Most frequent count: 2

Return an array where each element represents the count of the most frequent ID after each step. If the collection becomes empty at any point, return 0 for that step.

Input & Output

example_1.py — Basic Operations
$ Input: nums = [2, 3, 2, 1], freq = [3, 2, -2, 1]
Output: [3, 3, 2, 2]
💡 Note: Step 0: Add 3×ID(2) → {2:3} → max=3. Step 1: Add 2×ID(3) → {2:3,3:2} → max=3. Step 2: Remove 2×ID(2) → {2:1,3:2} → max=2. Step 3: Add 1×ID(1) → {2:1,3:2,1:1} → max=2.
example_2.py — Complete Removal
$ Input: nums = [5, 5, 3], freq = [1, -1, 1]
Output: [1, 0, 1]
💡 Note: Step 0: Add 1×ID(5) → {5:1} → max=1. Step 1: Remove 1×ID(5) → {} → max=0. Step 2: Add 1×ID(3) → {3:1} → max=1.
example_3.py — Large Frequencies
$ Input: nums = [1, 1, 1], freq = [100, 50, -25]
Output: [100, 150, 125]
💡 Note: Step 0: Add 100×ID(1) → {1:100} → max=100. Step 1: Add 50×ID(1) → {1:150} → max=150. Step 2: Remove 25×ID(1) → {1:125} → max=125.

Constraints

  • 1 ≤ nums.length == freq.length ≤ 105
  • 1 ≤ nums[i] ≤ 106
  • -105 ≤ freq[i] ≤ 105
  • freq[i] ≠ 0
  • The number of any ID will not become negative at any point

Visualization

Tap to expand
📊 Real-time Analytics DashboardContent TrackerID: 2👍 3ID: 3👍 2Trending Queue321📈 Max Engagement3Current Highest🔄 Update Process1. Hash Map Update2. Heap Push3. Clean Outdated4. Get Maximum⚡ O(n log n) time complexity with efficient real-time updates
Understanding the Visualization
1
Content Gets Engagement
When content receives likes/shares, we add it to our trending tracker
2
Content Loses Popularity
When content becomes less relevant, we reduce its engagement count
3
Track Top Engagement
We always need to know the highest engagement count among all trending content
4
Efficient Updates
Use hash map + heap for fast updates and maximum tracking
Key Takeaway
🎯 Key Insight: Combine hash map's O(1) updates with heap's efficient maximum tracking, using lazy deletion to handle stale heap entries elegantly.
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 28 Apple 19
52.3K Views
High Frequency
~25 min Avg. Time
1.8K 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