KatInterview

Rolling-Window Chat Activity Tracker

Data Structure DesignAuto-graded · 13 test cases
HardAuto-gradedData Structure Designsliding-windowdequehash-map

Last updated

Build a bounded-memory sliding-window aggregator answering distinct-chat, distinct-user, and message-total queries from an operation log in amortized O(1).

Problem Statement

You are implementing the metrics layer of a messaging product. It measures activity inside a moving time window and answers on-demand questions about that window.

You are given an operation log: a list operations of operation names and a parallel list arguments, where arguments[i] holds the parameters for operations[i]. Replay the log in order and return a list results, one entry per operation.

The tracked entity is configured by the first operation, "__init__", whose single argument is the window length w in seconds. An event recorded at time ts is considered live at query time t exactly when t - w < ts <= t (left boundary exclusive, right boundary inclusive). Recorded event timestamps never decrease over the log; query times may be any non-negative integer, including values far past the last recorded event.

Supported operations and their argument lists:

  • "__init__" with [w]: creates the tracker with window length w. Produces None.
  • "record_message" with [timestamp, chat_id, user_id]: a message from user_id in chat_id at unix time timestamp. Produces None.
  • "get_active_chat_count" with [current_time]: number of distinct chats with at least one live message in the window ending at current_time.
  • "get_active_user_count" with [current_time]: number of distinct users with at least one live message in any chat.
  • "get_active_user_count" with [current_time, chat_id]: number of distinct users with at least one live message specifically in chat_id.
  • "get_total_messages" with [current_time]: number of live messages across all chats.

For every query operation, produce the integer result. For "__init__" and "record_message", produce None.

Memory must remain proportional to the currently live window: identifiers whose activity drops to zero must be removed, so a long log full of short-lived identifiers does not grow the state without bound.

Constraints

  • 1 <= w <= 10^6.
  • operations contains exactly one "__init__" entry, at index 0; every other operation comes after it.
  • 1 <= len(operations) == len(arguments) <= 2 * 10^5.
  • timestamp and current_time are non-negative integers; recorded timestamps are non-decreasing across the log.
  • Query times are not necessarily non-decreasing and may precede earlier query times (though not earlier than the window they describe allows being evicted — since eviction is monotone in the timestamps recorded, a query only needs to reflect its own time).
  • chat_id and user_id are strings of length 1 to 32.
  • Aim for amortized O(1) per operation, with O(E) space where E is the number of live events.

Examples

Example 1

Input: replay_activity_log(['__init__', 'record_message', 'record_message', 'record_message', 'get_total_messages', 'get_active_chat_count', 'get_active_user_count', 'get_active_user_count', 'get_total_messages', 'get_active_user_count', 'get_active_user_count', 'get_active_chat_count', 'get_total_messages'], [[300], [100, 'c1', 'u1'], [150, 'c1', 'u2'], [200, 'c2', 'u1'], [250], [250], [250], [250, 'c1'], [420], [420, 'c1'], [420], [501], [501]])

Output: [None, None, None, None, 3, 2, 2, 2, 2, 1, 2, 0, 0]

Hints

Hint 1

Recorded timestamps only move forward, so the earliest recorded event is always the next one to expire — a single FIFO queue drained from the front is enough, no heap needed.

Hint 2

Distinct counts need presence structures that shrink on expiry: a per-chat map of user to message count, plus a global map from user to the set of chats where they still have live messages.

Hint 3

Every expiry must undo the recording step exactly: decrement the per-chat user count, drop the user from the user's chat set when it hits zero, remove the chat entirely when it has no users left, and delete any container that becomes empty.