KatInterview

Sliding Window Score Monitor for Support Chats

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designsliding-windowhash-mapqueue

Last updated

Process a stream of score updates and queries, returning the rolling average and maximum over the latest score of each chat inside a half-open time window.

A support platform scores its chats as they happen. You must simulate a monitor that keeps only recent scores and answers questions about them.

Implement score_window_monitor(window_size, commands, params). It replays a list of commands in order and returns the answers to the query commands.

Commands

commands[i] is one of three strings, and params[i] holds its arguments:

Commandparams[i]Effect
"record_score"[timestamp, chat_id, score]Stores score for chat_id at timestamp. If chat_id already has a record that is still inside the window, the new score and timestamp replace the old ones.
"get_average_score"[timestamp]Returns the mean score of all active chats.
"get_max_score"[timestamp]Returns the highest score among all active chats.

Rules

  • A chat is active at query time t if its most recent record has timestamp s with t - window_size < s <= t. The window is open at the left end and closed at the right end, so a record exactly window_size seconds old has already expired.
  • Each chat counts once, using only its latest record. Older records of the same chat are ignored.
  • Timestamps never decrease from one command to the next.
  • If a chat's earlier record has expired, a new record_score for that chat simply creates a fresh record.
  • get_average_score returns a float rounded to 2 decimal places, or 0.0 if no chat is active.
  • get_max_score returns an integer, or -1 if no chat is active. A score of 0 is a real score and is not the same as "no chats".
  • record_score produces no output.

Output

Return a list containing one value per query command (get_average_score or get_max_score), in the order the queries appear.

Constraints

  • 1 <= window_size <= 86400
  • 1 <= timestamp <= 10^9, and timestamps are non-decreasing across commands
  • 1 <= len(chat_id) <= 32
  • 0 <= score <= 100
  • At most 100,000 commands in total
  • len(commands) == len(params)

Examples

Example 1

Input: score_window_monitor(10, ['record_score', 'record_score', 'get_average_score', 'get_max_score', 'record_score', 'get_average_score', 'get_max_score', 'get_average_score', 'get_max_score'], [[1, 'conv_A', 80], [3, 'conv_B', 90], [5], [5], [8, 'conv_A', 95], [8], [12], [14], [14]])

Output: [85, 90, 92.5, 95, 95, 95]

Example 2

Input: score_window_monitor(1, ['record_score', 'get_average_score', 'get_max_score'], [[5, 'x', 0], [5], [5]])

Output: [0, 0]

Hints

Hint 1

Because timestamps never decrease, the oldest records are always the first to expire. A queue ordered by arrival lets you drop expired records from the front.

Hint 2

A chat can be updated while its older record is still waiting in the queue. When that old entry reaches the front, make sure it does not delete the newer record. Compare timestamps with what is currently stored for that chat.

Hint 3

Keep a running sum and a count of active chats for the average. Scores are only 0 to 100, so a 101-slot tally of how many active chats hold each score gives the maximum with a short scan from the top.