KatInterview

Roster Coverage and Rating Leaderboard Simulator

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designdesignintervalshash-map

Last updated

Simulate a command log against a stateful tracker that merges per-agent time intervals and ranks agents by average rating with alphabetical tie-breaking.

Roster Coverage and Rating Leaderboard

A support team keeps a small in-memory tracker for its agents. It records the time ranges each agent worked and the satisfaction scores customers gave them. You will implement the tracker and a driver function that replays a log of commands against it.

The tracker

Create a class AgentTracker. Its method names must match the command names below exactly, because the driver looks them up by name.

  • add_shift(agent_id, start, end) records that the agent worked during the closed range [start, end]. Ranges can overlap and can arrive in any order. It returns None.
  • get_merged_shifts(agent_id) returns the agent's total coverage as a list of [start, end] pairs.
    • The pairs must be pairwise disjoint and sorted by start in ascending order.
    • Ranges that overlap or merely touch at an endpoint must be combined. For example, [1, 3] and [3, 5] become [1, 5].
    • An agent with no recorded shifts yields [].
  • record_rating(agent_id, score) stores one integer rating for the agent. It returns None.
  • get_top_agents(k) returns up to k (agent_id, average_rating) pairs.
    • The average is the sum of the agent's ratings divided by the number of ratings, as a float.
    • Order by average descending. If two averages are equal, order by agent_id ascending in lexicographic order.
    • Agents with no ratings are never included.
    • If fewer than k agents have ratings, return all of them. If there are none, or if k <= 0, return [].

The driver function

Implement the function whose signature is given in the starter code. It takes:

  • operations: a list of command names, each one of the four method names above.
  • arguments: a list of the same length. arguments[i] is the list of positional arguments for operations[i].

The function creates one fresh AgentTracker, executes the commands in order, and returns a list of the same length. Entry i is the value returned by command i, so commands that return nothing contribute None.

Constraints

  • operations and arguments have the same length, at most 20,000.
  • agent_id is a string. start and end are integers with start <= end. Ratings are integers.
  • get_merged_shifts must not expose internal state: modifying a returned list must not affect the tracker.
  • Equal averages must be treated as ties even if they come from different counts (for example 8/2 and 4/1).

Examples

Example 1

Input: replay_agent_log(['add_shift', 'add_shift', 'add_shift', 'get_merged_shifts', 'record_rating', 'record_rating', 'record_rating', 'record_rating', 'get_top_agents'], [['alice', 9, 12], ['alice', 11, 14], ['alice', 16, 18], ['alice'], ['alice', 5], ['alice', 3], ['bob', 4], ['charlie', 5], [2]])

Output: [None, None, None, [[9, 14], [16, 18]], None, None, None, None, [['charlie', 5], ['alice', 4]]]

Hints

Hint 1

Treat shifts and ratings as two independent pieces of state, each in a dictionary keyed by agent id.

Hint 2

For shifts, either store raw ranges and sort and sweep them on query, or keep each agent's list sorted and disjoint and fold in every new range. Use `<=` when comparing a start to the current end so touching ranges merge.

Hint 3

For ratings, keep only a running total and a count per agent. To rank, sort with the key `(-average, agent_id)` and slice the first `max(0, k)` entries.