Count Mentions Per User - Problem

You are given an integer numberOfUsers representing the total number of users and an array events of size n × 3. Each events[i] can be either of the following two types:

Message Event: ["MESSAGE", "timestamp_i", "mentions_string_i"]
This event indicates that a set of users was mentioned in a message at timestamp i. The mentions_string_i string can contain one of the following tokens:

  • id<number>: where <number> is an integer in range [0, numberOfUsers - 1]. There can be multiple ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.
  • ALL: mentions all users.
  • HERE: mentions all online users.

Offline Event: ["OFFLINE", "timestamp_i", "id_i"]
This event indicates that the user id_i had become offline at timestamp i for 60 time units. The user will automatically be online again at time timestamp_i + 60.

Return an array mentions where mentions[i] represents the number of mentions the user with id i has across all MESSAGE events. All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp.

Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately.

Input & Output

Example 1 — Basic Message and Offline Events
$ Input: numberOfUsers = 3, events = [["MESSAGE","1","id0 id1"],["OFFLINE","2","1"],["MESSAGE","3","HERE"]]
Output: [2,1,1]
💡 Note: First message mentions users 0 and 1. User 1 goes offline at t=2. Second message at t=3 uses HERE (mentions online users only: 0 and 2). Final counts: user 0 gets 2 mentions, user 1 gets 1 mention, user 2 gets 1 mention.
Example 2 — ALL Mentions
$ Input: numberOfUsers = 2, events = [["MESSAGE","5","ALL"],["OFFLINE","10","0"],["MESSAGE","15","ALL"]]
Output: [2,2]
💡 Note: First ALL mentions both users. User 0 goes offline. Second ALL still mentions ALL users (even offline ones). Both users get 2 mentions each.
Example 3 — Duplicate Mentions
$ Input: numberOfUsers = 2, events = [["MESSAGE","1","id0 id0 id1"]]
Output: [2,1]
💡 Note: User 0 is mentioned twice in the same message (duplicates count separately), user 1 is mentioned once.

Constraints

  • 1 ≤ numberOfUsers ≤ 1000
  • 1 ≤ events.length ≤ 1000
  • 0 ≤ timestampi ≤ 109
  • Each mention string contains valid tokens only

Visualization

Tap to expand
Count Mentions Per User ProblemInput EventsMESSAGE: id0 id1OFFLINE: user 1MESSAGE: HEREProcessingTrack online statusParse mention stringsCount per userOutputUser 0: 2 mentionsUser 1: 1 mentionUser 2: 1 mentionKey Challenge: HERE mentions only online usersMust track who is online/offline at each timestampResult: [2, 1, 1]
Understanding the Visualization
1
Input
Events with timestamps, message mentions, and offline periods
2
Process
Handle events chronologically, track user status
3
Output
Array with mention count per user
Key Takeaway
🎯 Key Insight: Process events chronologically while efficiently tracking user online/offline status to handle HERE mentions correctly
Asked in
Meta 35 Amazon 28 Google 22 Microsoft 18
23.4K Views
Medium Frequency
~25 min Avg. Time
845 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