Count User Mentions Across Timestamped Message and Offline Events

Quick Overview

Simulate a log of message and offline events to count how many times each user is mentioned, where ALL, HERE and explicit id lists follow different rules. Tests sorting events with a tie-break, tracking a timed offline window and careful parsing of string fields.

Count User Mentions Across Timestamped Message and Offline Events

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer `number_of_users`, the total number of users (ids `0` to `number_of_users - 1`), and an array `events` of size `n`. Each `events[i]` is one of: - `["MESSAGE", "<content>", "<timestamp>"]`: a message event with a content string and a timestamp. - `["OFFLINE", "<id>", "<timestamp>"]`: the user with the given id goes offline at the given timestamp and stays offline for 60 time units. The content of a message event is one of: - `"ALL"`: the message mentions all users. - `"HERE"`: the message mentions all online users. - A space-separated list of user ids, such as `"id0 id2 id4"`: the message mentions the listed users. Return an array `mentions` of size `number_of_users` where `mentions[i]` is the number of times user `i` is mentioned across all `MESSAGE` events. All users start online, and a user who goes offline comes back online 60 time units after the timestamp of the `OFFLINE` event. ### Function Signature ```python def count_mentions(number_of_users: int, events: list[list[str]]) -> list[int]: ``` ### Rules - Every event is a list of three strings `[type, content, timestamp]`. The timestamp is a decimal integer written as a string, and timestamps are compared as integers. - In an `OFFLINE` event the content is the bare user id, such as `"0"`. In a message id list each token has the form `id<number>`, such as `"id3"`. - Events are not necessarily given in timestamp order. Process them in increasing timestamp order. When an `OFFLINE` event and a `MESSAGE` event share a timestamp, the `OFFLINE` event takes effect first. - A user who goes offline at time `t` is offline for any message with a timestamp from `t` up to `t + 59`, and online again for a message at `t + 60` or later. - `"ALL"` mentions every user, online or offline. `"HERE"` mentions only the users who are online at that message's timestamp. An id list mentions the listed users whether or not they are online. - Each token in an id list counts as one mention, so an id that appears twice in the same message adds 2 to that user's count. ### Constraints - `1 <= number_of_users <= 100` - `1 <= len(events) <= 100` - `len(events[i]) == 3` - `events[i][0]` is `"MESSAGE"` or `"OFFLINE"`. - `1 <= int(events[i][2]) <= 10^5` - The number of `MESSAGE` events with content `"HERE"` or `"ALL"` is at most 100. - If `events[i][0] == "MESSAGE"` and the content is an id list, it holds between 1 and 100 ids, each `id<number>` with `0 <= number < number_of_users`. - If `events[i][0] == "OFFLINE"`, `events[i][1]` is a valid user id, and that user is online at the event's timestamp. ### Examples ```text Input: number_of_users = 2, events = [["MESSAGE","HERE","1"],["OFFLINE","0","2"],["MESSAGE","HERE","3"],["MESSAGE","ALL","4"]] Output: [2,3] ``` At timestamp 1 both users are online, so "HERE" mentions both. User 0 goes offline at timestamp 2 (until 62), so "HERE" at timestamp 3 mentions only user 1, and "ALL" at timestamp 4 mentions both, giving user 0 a total of 2 and user 1 a total of 3. ```text Input: number_of_users = 3, events = [["MESSAGE","id0 id1","5"],["MESSAGE","ALL","10"]] Output: [2,2,1] ``` The message at timestamp 5 mentions users 0 and 1, and "ALL" at timestamp 10 mentions all three users. ```text Input: number_of_users = 2, events = [["MESSAGE","HERE","70"],["MESSAGE","id1 id1","20"],["OFFLINE","1","10"],["MESSAGE","HERE","10"]] Output: [2,3] ``` In timestamp order, the `OFFLINE` event at 10 runs before "HERE" at 10, so that message reaches only user 0, and "id1 id1" at 20 still mentions offline user 1 twice. User 1 is back online at 10 + 60 = 70, so the last "HERE" mentions both users.

Overview: Simulate a log of message and offline events to count how many times each user is mentioned, where ALL, HERE and explicit id lists follow different rules. Tests sorting events with a tie-break, tracking a timed offline window and careful parsing of string fields.

Stripe logo
Stripe
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer number_of_users, the total number of users (ids 0 to number_of_users - 1), and an array events of size n. Each events[i] is one of:

  • ["MESSAGE", "<content>", "<timestamp>"] : a message event with a content string and a timestamp.
  • ["OFFLINE", "<id>", "<timestamp>"] : the user with the given id goes offline at the given timestamp and stays offline for 60 time units.

The content of a message event is one of:

  • "ALL" : the message mentions all users.
  • "HERE" : the message mentions all online users.
  • A space-separated list of user ids, such as "id0 id2 id4" : the message mentions the listed users.

Return an array mentions of size number_of_users where mentions[i] is the number of times user i is mentioned across all MESSAGE events. All users start online, and a user who goes offline comes back online 60 time units after the timestamp of the OFFLINE event.

Function Signature

def count_mentions(number_of_users: int, events: list[list[str]]) -> list[int]:

Rules

  • Every event is a list of three strings [type, content, timestamp] . The timestamp is a decimal integer written as a string, and timestamps are compared as integers.
  • In an OFFLINE event the content is the bare user id, such as "0" . In a message id list each token has the form id<number> , such as "id3" .
  • Events are not necessarily given in timestamp order. Process them in increasing timestamp order. When an OFFLINE event and a MESSAGE event share a timestamp, the OFFLINE event takes effect first.
  • A user who goes offline at time t is offline for any message with a timestamp from t up to t + 59 , and online again for a message at t + 60 or later.
  • "ALL" mentions every user, online or offline. "HERE" mentions only the users who are online at that message's timestamp. An id list mentions the listed users whether or not they are online.
  • Each token in an id list counts as one mention, so an id that appears twice in the same message adds 2 to that user's count.

Constraints

  • 1 <= number_of_users <= 100
  • 1 <= len(events) <= 100
  • len(events[i]) == 3
  • events[i][0] is "MESSAGE" or "OFFLINE" .
  • 1 <= int(events[i][2]) <= 10^5
  • The number of MESSAGE events with content "HERE" or "ALL" is at most 100.
  • If events[i][0] == "MESSAGE" and the content is an id list, it holds between 1 and 100 ids, each id<number> with 0 <= number < number_of_users .
  • If events[i][0] == "OFFLINE" , events[i][1] is a valid user id, and that user is online at the event's timestamp.

Examples

Input: number_of_users = 2, events = [["MESSAGE","HERE","1"],["OFFLINE","0","2"],["MESSAGE","HERE","3"],["MESSAGE","ALL","4"]]
Output: [2,3]

At timestamp 1 both users are online, so "HERE" mentions both. User 0 goes offline at timestamp 2 (until 62), so "HERE" at timestamp 3 mentions only user 1, and "ALL" at timestamp 4 mentions both, giving user 0 a total of 2 and user 1 a total of 3.

Input: number_of_users = 3, events = [["MESSAGE","id0 id1","5"],["MESSAGE","ALL","10"]]
Output: [2,2,1]

The message at timestamp 5 mentions users 0 and 1, and "ALL" at timestamp 10 mentions all three users.

Input: number_of_users = 2, events = [["MESSAGE","HERE","70"],["MESSAGE","id1 id1","20"],["OFFLINE","1","10"],["MESSAGE","HERE","10"]]
Output: [2,3]

In timestamp order, the OFFLINE event at 10 runs before "HERE" at 10, so that message reaches only user 0, and "id1 id1" at 20 still mentions offline user 1 twice. User 1 is back online at 10 + 60 = 70, so the last "HERE" mentions both users.