Design an in-memory customer support agent leaderboard with monthly rankings

Quick Overview

Design an in-memory leaderboard that records 1 to 5 ratings for customer support agents, ranks every agent by average rating with alphabetical tie-breaks, and finds the best agent in a given month. It tests data modeling for aggregate queries, composite sort keys, rounding and empty-data edge cases.

Design an in-memory customer support agent leaderboard with monthly rankings

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory system that tracks and displays a leaderboard for customer support agents. Each customer interaction produces a rating from 1 to 5 for the agent who handled it, recorded along with the date of the interaction. The system should support recording ratings, retrieving all agents sorted by their average rating in descending order, finding the best agent for a specific month, and querying an individual agent's average rating. When multiple agents share the same average rating, order them lexicographically by name. If no data exists for a given query, return an empty result. Implement a class `SupportLeaderboard` with these methods: - `add_rating(agent: str, rating: int, date: str) -> None`: record a rating (1 to 5) for the given agent on the given date, in `YYYY-MM-DD` format. - `get_top_agents() -> List[str]`: return all agent names sorted by average rating in descending order. Ties are broken lexicographically by agent name. - `get_best_agent_of_month(month: str) -> str`: return the name of the agent with the highest average rating for the given month, in `YYYY-MM` format. Ties are broken lexicographically. Return an empty string if no ratings exist for that month. - `get_agent_average(agent: str) -> float`: return the agent's average rating, rounded to 2 decimal places. Return `0.0` if the agent has no recorded ratings. Example 1 (three agents rated across two months): ```python board = SupportLeaderboard() board.add_rating("Carol", 4, "2024-05-02") board.add_rating("Alice", 5, "2024-05-10") board.add_rating("Bob", 4, "2024-05-12") board.add_rating("Alice", 3, "2024-06-01") board.add_rating("Bob", 5, "2024-06-03") board.add_rating("Carol", 5, "2024-06-15") board.add_rating("Alice", 5, "2024-06-20") board.get_top_agents() # -> ["Bob", "Carol", "Alice"] board.get_best_agent_of_month("2024-05") # -> "Alice" board.get_best_agent_of_month("2024-06") # -> "Bob" board.get_best_agent_of_month("2024-07") # -> "" board.get_agent_average("Alice") # -> 4.33 ``` Bob and Carol both average 4.5 overall and Alice averages 13/3, about 4.33, so the ranking is Bob, Carol, Alice, with the name tie-break putting Bob ahead of Carol. In June, Bob and Carol both average 5.0 and Alice averages 4.0, so the tie-break picks Bob. In May, Alice's single 5 is the highest rating, and July has no ratings at all. Example 2 (queries on an empty leaderboard): ```python board = SupportLeaderboard() # empty leaderboard board.get_top_agents() # -> [] (no agents yet) board.get_best_agent_of_month("2024-06") # -> "" (no ratings for June) board.add_rating("Dave", 3, "2024-06-01") # Dave: [3] board.get_agent_average("Dave") # -> 3.0 board.get_agent_average("Unknown") # -> 0.0 (agent not found) ``` ```hint What to keep per agent Every query is an average over some group of ratings. Think about which totals you could update in constant time when a rating arrives, so no query has to rescan old ratings. ``` ```hint Ordering with ties One sort key can encode both rules at once: descending average and ascending name. Watch what happens to ties if you rank on rounded values. ``` ### Constraints & Assumptions - `1 <= rating <= 5` - Dates use `YYYY-MM-DD` format and month queries use `YYYY-MM` format. - Agent names are non-empty alphanumeric strings. - At most ${10^4}$ calls in total across all methods. ### Clarifying Questions to Ask - Should the rankings compare exact averages, or the averages rounded to 2 decimal places? - At an exact midpoint such as 3.125, should `get_agent_average` round half up or half to even? - Is lexicographic order case-sensitive, so that `"Zoe"` sorts before `"alice"`? - Are ratings append-only, or can a rating be edited or deleted later? - Is the class used from a single thread, or can several threads record ratings at once? ### What a Strong Answer Covers - A data model that answers each query without rescanning every rating, with the time cost of each method stated. - Correct tie-breaking in both ranking methods and the empty results when there is no data. - How a `YYYY-MM-DD` date maps to its `YYYY-MM` month for the monthly query. - Rounding to 2 decimal places only at output, and how the chosen rounding mode behaves at a midpoint. - The trade-off between storing raw ratings and storing running totals. ### Follow-up Questions - How would you keep `get_top_agents` fast if it is called far more often than `add_rating`? - How would the design change if ratings can be edited or deleted? - How would you make the class safe to use from several threads? - How would you find the best agent over an arbitrary date range, such as the last 30 days?

Overview: Design an in-memory leaderboard that records 1 to 5 ratings for customer support agents, ranks every agent by average rating with alphabetical tie-breaks, and finds the best agent in a given month. It tests data modeling for aggregate queries, composite sort keys, rounding and empty-data edge cases.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory system that tracks and displays a leaderboard for customer support agents. Each customer interaction produces a rating from 1 to 5 for the agent who handled it, recorded along with the date of the interaction.

The system should support recording ratings, retrieving all agents sorted by their average rating in descending order, finding the best agent for a specific month, and querying an individual agent's average rating.

When multiple agents share the same average rating, order them lexicographically by name. If no data exists for a given query, return an empty result.

Implement a class SupportLeaderboard with these methods:

  • add_rating(agent: str, rating: int, date: str) -> None : record a rating (1 to 5) for the given agent on the given date, in YYYY-MM-DD format.
  • get_top_agents() -> List[str] : return all agent names sorted by average rating in descending order. Ties are broken lexicographically by agent name.
  • get_best_agent_of_month(month: str) -> str : return the name of the agent with the highest average rating for the given month, in YYYY-MM format. Ties are broken lexicographically. Return an empty string if no ratings exist for that month.
  • get_agent_average(agent: str) -> float : return the agent's average rating, rounded to 2 decimal places. Return 0.0 if the agent has no recorded ratings.

Example 1 (three agents rated across two months):

board = SupportLeaderboard()
board.add_rating("Carol", 4, "2024-05-02")
board.add_rating("Alice", 5, "2024-05-10")
board.add_rating("Bob", 4, "2024-05-12")
board.add_rating("Alice", 3, "2024-06-01")
board.add_rating("Bob", 5, "2024-06-03")
board.add_rating("Carol", 5, "2024-06-15")
board.add_rating("Alice", 5, "2024-06-20")

board.get_top_agents()                     # -> ["Bob", "Carol", "Alice"]
board.get_best_agent_of_month("2024-05")   # -> "Alice"
board.get_best_agent_of_month("2024-06")   # -> "Bob"
board.get_best_agent_of_month("2024-07")   # -> ""
board.get_agent_average("Alice")           # -> 4.33

Bob and Carol both average 4.5 overall and Alice averages 13/3, about 4.33, so the ranking is Bob, Carol, Alice, with the name tie-break putting Bob ahead of Carol. In June, Bob and Carol both average 5.0 and Alice averages 4.0, so the tie-break picks Bob. In May, Alice's single 5 is the highest rating, and July has no ratings at all.

Example 2 (queries on an empty leaderboard):

board = SupportLeaderboard()                # empty leaderboard
board.get_top_agents()                      # -> [] (no agents yet)
board.get_best_agent_of_month("2024-06")    # -> "" (no ratings for June)
board.add_rating("Dave", 3, "2024-06-01")   # Dave: [3]
board.get_agent_average("Dave")             # -> 3.0
board.get_agent_average("Unknown")          # -> 0.0 (agent not found)

Constraints & Assumptions

  • 1 <= rating <= 5
  • Dates use YYYY-MM-DD format and month queries use YYYY-MM format.
  • Agent names are non-empty alphanumeric strings.
  • At most 104{10^4} calls in total across all methods.

Clarifying Questions to Ask Guidance

  • Should the rankings compare exact averages, or the averages rounded to 2 decimal places?
  • At an exact midpoint such as 3.125, should get_agent_average round half up or half to even?
  • Is lexicographic order case-sensitive, so that "Zoe" sorts before "alice" ?
  • Are ratings append-only, or can a rating be edited or deleted later?
  • Is the class used from a single thread, or can several threads record ratings at once?

What a Strong Answer Covers Guidance

  • A data model that answers each query without rescanning every rating, with the time cost of each method stated.
  • Correct tie-breaking in both ranking methods and the empty results when there is no data.
  • How a YYYY-MM-DD date maps to its YYYY-MM month for the monthly query.
  • Rounding to 2 decimal places only at output, and how the chosen rounding mode behaves at a midpoint.
  • The trade-off between storing raw ratings and storing running totals.

Follow-up Questions Guidance

  • How would you keep get_top_agents fast if it is called far more often than add_rating ?
  • How would the design change if ratings can be edited or deleted?
  • How would you make the class safe to use from several threads?
  • How would you find the best agent over an arbitrary date range, such as the last 30 days?