cswork / libraryOpen practice workspace

Coding questions · Atlassian

Implement agent voting with average and sorting

This question evaluates the ability to design in-memory data structures and algorithms for recording and aggregating agent votes, including computing average ratings, sorting by aggregate score with tie-breaking, handling update semantics for repeated votes, and accounting for items with no votes.

Design an in-memory “agent voting” module that records ratings agents give to items (e.g., tickets/articles).

Requirements:

  • Each vote is: (agentId, itemId, rating) where rating is an integer (e.g., 1–5).
  • Support computing the average rating for a given itemId.
  • Support returning items sorted by average rating (descending). Specify how ties are broken (e.g., by itemId ascending).

Clarify behaviors:

  • If the same agent votes on the same item multiple times, should the latest vote replace the previous one?
  • What should be returned for items with no votes?

Provide the core data structures and methods, and analyze time/space complexity.

Agent Voting Averages

Record item ratings by agent, replacing repeat votes by the same agent, and return averages or sorted items.

Constraints

  • Ratings are integers

Examples

Example 1

Input: ((('vote', 'a', 'x', 5), ('vote', 'b', 'x', 3), ('avg', 'x')),)
Output: [4.0]

Explanation: Average from two agents.

Example 2

Input: ((('vote', 'a', 'x', 5), ('vote', 'a', 'x', 1), ('avg', 'x')),)
Output: [1.0]

Explanation: Latest vote replaces previous.

Hints

  1. Maintain per-agent item votes plus per-item totals and voter counts.