Ctrl K

Who Got Mentioned How Often

Medium
A team chat has user_count members numbered from 0 to user_count - 1. You are given an event log log where each event is a triple of strings of one of two kinds: - ["MESSAGE", time, text] is a message with mentions. text is either several id<number> tokens separated by spaces (numbers may repeat, and members who are away can still be mentioned this way), or ALL, which mentions every member, or HERE, which mentions everyone who is currently around. - ["OFFLINE", time, number] means member number steps away for exactly 60 time units: at time + 60 they are automatically back. Everyone starts out present. The log is not necessarily sorted by time. When status changes and messages happen at the same moment, the status changes are applied before the messages are handled. Return an array whose i-th element is the total number of times member i was mentioned. Every mention counts separately, even when a number appears several times in one message.
Example 1
user_count=3, log=[['MESSAGE', '5', 'id2 id2 id0'], ['OFFLINE', '7', '2'], ['MESSAGE', '30', 'HERE']] → [2, 1, 2]

At time 5 member 2 is mentioned twice and member 0 once. At time 7 member 2 goes away until time 67, so the HERE at time 30 reaches only members 0 and 1.

Example 2
user_count=2, log=[['OFFLINE', '40', '1'], ['MESSAGE', '100', 'HERE'], ['MESSAGE', '41', 'ALL']] → [2, 2]

The events are not listed in time order. ALL at time 41 hits both members even though member 1 is away. By time 100 member 1 is back (40 + 60 = 100), and status changes are handled before messages at the same moment, so the HERE reaches both as well.

Example 3
user_count=1, log=[['OFFLINE', '3', '0'], ['MESSAGE', '3', 'HERE'], ['MESSAGE', '3', 'id0']] → [1]

Member 0 goes away at time 3 before that moment's messages are handled, so HERE misses them, while the direct mention still counts.

Constraints
  • 1 ≤ user_count ≤ 100
  • 1 ≤ len(log) ≤ 100
  • log[i][0] is "MESSAGE" or "OFFLINE"
  • 1 ≤ time ≤ 10⁵
  • A message with numbers has 1 to 100 tokens, each number between 0 and user_count - 1
  • An OFFLINE event only arrives for a member who is present at that moment
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSorted Parabola Values MediumChain of Multiples MediumMeet at One Value MediumClosest Clock Readings MediumUp or Down for Every Post Medium
loading Python…
Input
user_count=3, log=[['MESSAGE', '5', 'id2 id2 id0'], ['OFFLINE', '7', '2'], ['MESSAGE', '30', 'HERE']]
Expected
[2, 1, 2]

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.