Support Desk
Problem
Design a SupportDesk that registers skilled agents, creates support tickets, and routes each ticket to a capable agent. Several threads may call the same desk, so assignment and workload changes must remain consistent.
Requirements
- Register agents.
addAgentcreates an agent whose skills are exactly the supplied distinct strings. - Open tickets.
createTicketcreates an open, unassigned ticket that requires one skill. - Match skills.
assignTicketconsiders only agents with the ticket's required skill. If none exists, it returns the empty string and leaves the ticket open. - Balance work. Among eligible agents,
assignTicketchooses the one with the fewest assigned unresolved tickets. A tie goes to the lexicographically smallest agent ID. The method assigns the ticket once and returns that ID. - Resolve tickets.
resolveTicketresolves an assigned ticket and removes it from its owner's unresolved workload while retaining that owner. - Coordinate threads. Public calls are linearizable on one shared desk. Every ticket has at most one owner, and each workload equals the number of assigned unresolved tickets owned by that agent.
API
| Signature | Returns | Behavior |
|---|---|---|
SupportDesk() | Not applicable | Creates an empty support desk. |
addAgent(agentId: string, skills: string[]) | void | Registers one agent and its skill set. |
createTicket(ticketId: string, requiredSkill: string) | void | Creates one open ticket. |
assignTicket(ticketId: string) | string | Assigns the ticket and returns the chosen agent ID, or returns the empty string when no agent is eligible. |
resolveTicket(ticketId: string) | void | Resolves one assigned ticket. |
Examples
| Operation | Result |
|---|---|
addAgent("ana", ["payments", "returns"]) | |
addAgent("bo", ["payments"]) | |
createTicket("t1", "payments") | |
createTicket("t2", "payments") | |
assignTicket("t1") | "ana" |
assignTicket("t2") | "bo" |
resolveTicket("t1") | |
createTicket("t3", "returns") | |
assignTicket("t3") | "ana" |
If two threads assign two payment tickets while both agents have zero workload, the returned agent IDs are "ana" and "bo" in either ticket-to-agent pairing.
Constraints
- Agent IDs, ticket IDs, and skill strings are non-empty ASCII strings of at most 64 characters.
- Agent IDs and ticket IDs are unique when registered or created.
- Each agent receives between 1 and 32 distinct skills.
assignTicketis called only for an existing open ticket.resolveTicketis called only for an existing assigned ticket.- At most 1,000 agents, 10,000 tickets, and 50,000 public method calls occur in one testcase.
- Inputs satisfy these constraints.
Notes
Strings are case-sensitive, and lexicographic order uses their ordinary character order. Calls keep program order within each thread; across threads, any linearizable interleaving is valid. Methods may wait for finite in-progress work but never wait for an eligible agent, and no fairness or arrival order is promised.