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 INPUT numberOfUsers = 3 events array: [0] MESSAGE, ts=1 "id0 id1" id0 id1 [1] OFFLINE, ts=2 user id=1 offline until ts=62 [2] MESSAGE, ts=3 "HERE" (online users only) Initial User Status (all online): User 0 User 1 User 2 Green = Online ALGORITHM STEPS 1 Sort Events by Timestamp Process OFFLINE before MESSAGE at same timestamp 2 Process ts=1: MESSAGE "id0 id1" --> user0++, user1++ mentions = [1, 1, 0] 3 Process ts=2: OFFLINE User 1 goes offline Online until ts=62 4 Process ts=3: MESSAGE "HERE" --> online users only User 0, User 2 are online mentions = [2, 1, 1] Timeline: ts=1 MSG ts=2 OFF ts=3 HERE FINAL RESULT Output Array: 2 User 0 1 User 1 1 User 2 [2, 1, 1] Mention Breakdown: User 0: 1 + 1 = 2 (id0 + HERE) User 1: 1 + 0 = 1 (id1, offline for HERE) User 2: 0 + 1 = 1 (HERE only) OK - Verified Key Insight: Process events chronologically, handling OFFLINE events before MESSAGE events at the same timestamp. Track online status for each user (offline for 60 time units). "HERE" only mentions currently online users, while "ALL" mentions everyone. Parse "id<n>" tokens to get specific user IDs. Count duplicates separately. TutorialsPoint - Count Mentions Per User | Optimized Event Processing with Timeline
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