KatInterview

Chat Activity Tracker with a Rolling 15-Minute Window

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designdesignhash-mapheap

Last updated

Simulate a chat activity tracker with a closed rolling window, touch/close liveness rules, and out-of-order events, using bounded-memory eviction.

You are simulating an in-memory, single-threaded service that tracks chat activity. It processes a list of operations in order and reports the results of the queries.

Events

Each event has four fields:

  • user (string): the user who owns the chat.
  • chat (int): a chat id that is unique only within its user, so a chat is identified by the pair (user, chat).
  • ts (int): the event time in whole minutes.
  • kind (string): either "touch" or "close".

The window

Let T be the largest ts seen so far across all ingested events from all users. The active window is the closed interval [T - 14, T]. For example, if T = 20, the window is [6, 20], and an event at minute 6 still counts.

Events can arrive out of order, so an event's ts may be smaller than T. T never decreases. An event whose ts falls below T - 14 is outside the window and has no effect. An event can also fall out of the window later, when another event moves T forward.

Operations

operations is a list of operation names and arguments is a parallel list holding each operation's arguments. Process them in order.

  • "ingest" with arguments [user, chat, ts, kind]: record one event and update T first. It produces no output.
  • "count_recent" with arguments [user, chat]: output the number of touch events of that chat whose ts is in [T - 14, T]. Close events are not counted. Several touches with the same ts each count separately. Output 0 for an unknown chat or if nothing has been ingested yet.
  • "count_live" with arguments [user]: output the number of that user's chats that are live right now. Output 0 for an unknown user or if nothing has been ingested yet.

Live chats

A chat is live if and only if both conditions hold:

  1. It has at least one touch with ts in [T - 14, T].
  2. There is no close for that chat with a ts strictly greater than the chat's latest touch ts.

The comparison uses timestamps, never arrival order. A close with the same ts as the latest touch does not end the chat. A later touch with a ts at least as large as every close reactivates a closed chat. Closes whose ts has fallen out of the window can be ignored, because they can never be strictly greater than a touch that is still in the window.

Output

Return a list of integers containing the results of every count_recent and count_live operation, in the order the operations appear. ingest adds nothing to the list.

Constraints

  • operations and arguments have the same length, and every operation name is one of the three above.
  • Every ingest provides all four arguments, with kind either "touch" or "close".
  • Timestamps are integers and may arrive in any order, but T is defined as the running maximum.
  • Memory should stay proportional to the events inside the current window, not to the whole history.
  • Aim for efficient ingestion and queries (roughly logarithmic ingestion, and queries that cost at most the number of the queried user's recently active chats).

Examples

Example 1

Input: run_chat_tracker(['ingest', 'count_recent', 'count_live', 'ingest', 'count_live', 'ingest', 'count_live'], [['alice', 1, 2, 'touch'], ['alice', 1], ['alice'], ['alice', 1, 8, 'close'], ['alice'], ['alice', 1, 8, 'touch'], ['alice']])

Output: [1, 1, 0, 1]

Example 2

Input: run_chat_tracker(['ingest', 'ingest', 'count_live', 'ingest', 'count_live', 'count_recent'], [['alice', 1, 10, 'close'], ['alice', 1, 7, 'touch'], ['alice'], ['alice', 1, 12, 'touch'], ['alice'], ['alice', 1]])

Output: [0, 1, 2]

Hints

Hint 1

Keep per-chat state small: a map from timestamp to touch count, plus only the largest close timestamp. Older closes can never matter.

Hint 2

Because events can arrive out of order, use a min-heap keyed by timestamp to evict everything older than T - 14 whenever T advances, and drop chats that become empty.

Hint 3

A chat is live exactly when it has some touch left in the window and its largest close is not strictly greater than its largest touch. The window holds at most 15 distinct timestamps, so checking this per chat is cheap.