Count Mentions Per User - Problem

Imagine you're building a chat monitoring system for a social platform! Your task is to track how many times each user gets mentioned across all messages.

You're given numberOfUsers representing the total number of users and an array events containing two types of events:

๐Ÿ“จ Message Events

["MESSAGE", "timestamp", "mentions_string"] - A message was sent at the given timestamp with mentions. The mentions can be:

  • id<number> - Mentions specific user ID (e.g., "id0 id2 id0" mentions user 0 twice and user 2 once)
  • ALL - Mentions every single user
  • HERE - Mentions only currently online users

๐Ÿ”Œ Offline Events

["OFFLINE", "timestamp", "user_id"] - User goes offline for exactly 60 time units (returns online at timestamp + 60)

Goal: Return an array where result[i] = total mentions for user i across all MESSAGE events.

Note: All users start online, and status changes are processed before messages at the same timestamp.

Input & Output

example_1.py โ€” Basic mentions
$ Input: numberOfUsers = 3, events = [["MESSAGE","10","id0 id1"],["MESSAGE","20","ALL"],["MESSAGE","30","HERE"]]
โ€บ Output: [3, 3, 2]
๐Ÿ’ก Note: User 0: mentioned in first message (id0), second message (ALL), and third message (HERE) = 3 times. User 1: mentioned in first message (id1), second message (ALL), and third message (HERE) = 3 times. User 2: mentioned in second message (ALL) and third message (HERE) = 2 times.
example_2.py โ€” With offline users
$ Input: numberOfUsers = 2, events = [["OFFLINE","5","0"],["MESSAGE","10","HERE"],["MESSAGE","70","HERE"]]
โ€บ Output: [1, 2]
๐Ÿ’ก Note: User 0 goes offline at T=5 and comes back at T=65. First HERE message at T=10 only mentions user 1 (user 0 is offline). Second HERE message at T=70 mentions both users (user 0 is back online). Result: User 0 gets 1 mention, User 1 gets 2 mentions.
example_3.py โ€” Duplicate mentions
$ Input: numberOfUsers = 2, events = [["MESSAGE","10","id0 id0 id1"]]
โ€บ Output: [2, 1]
๐Ÿ’ก Note: The message "id0 id0 id1" mentions user 0 twice and user 1 once. Each mention is counted separately, so user 0 gets 2 mentions and user 1 gets 1 mention.

Visualization

Tap to expand
Chat Mention System Timeline๐Ÿ“… Event Timeline (Sorted by Timestamp)T=5 OFFLINET=10 MESSAGET=20 MESSAGET=70 MESSAGE๐Ÿ‘ฅ Online Status TrackerUser 0: T=5โ†’T=65โ˜• Offline for coffeeUser 1: Online๐ŸŸข AvailableofflineUntil[0] = 65, others online๐Ÿ’ฌ Mention Counter๐Ÿ“Š Running totals:User 0: 1User 1: 2User 2: 2โ€ข ALL โ†’ +1 for everyoneโ€ข HERE โ†’ +1 for online users only๐ŸŽฏ Key Algorithm Steps1. Sort events by timestamp โ†’ O(n log n)2. Process chronologically, track offline periods โ†’ O(n)3. For each MESSAGE: check online status + count mentions โ†’ O(m)๐Ÿ“ˆ Total: O(n log n + m) where m = total mention tokensโœ… Optimal for large inputs!๐Ÿ’ก Smart offline tracking avoids O(nยฒ) recalculation on every message!
Understanding the Visualization
1
Timeline Setup
Sort all events chronologically to process them in the order they actually happened
2
Status Tracking
Keep track of when each user will return from their coffee break (offline + 60 minutes)
3
Message Processing
For each announcement, count mentions: ALL (everyone), HERE (present staff), or specific names
4
Final Count
Accumulate all mentions for each user across the entire timeline
Key Takeaway
๐ŸŽฏ Key Insight: Instead of recalculating who's online for every message (O(nยฒ)), we maintain a smart mapping of when users return online, making the solution optimal at O(n log n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n + m)

O(n log n) for sorting events, O(m) for processing all mention tokens across all messages

n
2n
โšก Linearithmic
Space Complexity
O(n + u)

O(n) for offline tracking data structures, O(u) for result array where u = numberOfUsers

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค numberOfUsers โ‰ค 105
  • 0 โ‰ค events.length โ‰ค 105
  • Each event has exactly 3 elements
  • 0 โ‰ค timestampi โ‰ค 109
  • For OFFLINE events: 0 โ‰ค user_id โ‰ค numberOfUsers - 1
  • mentions_string contains only valid tokens separated by single spaces
  • Status changes are processed before messages at the same timestamp
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
43.2K Views
Medium-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