Problemsupport desk

Session: Sign in to solve

Solution.txt

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. addAgent creates an agent whose skills are exactly the supplied distinct strings.
  • Open tickets. createTicket creates an open, unassigned ticket that requires one skill.
  • Match skills. assignTicket considers 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, assignTicket chooses 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. resolveTicket resolves 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

SignatureReturnsBehavior
SupportDesk()Not applicableCreates an empty support desk.
addAgent(agentId: string, skills: string[])voidRegisters one agent and its skill set.
createTicket(ticketId: string, requiredSkill: string)voidCreates one open ticket.
assignTicket(ticketId: string)stringAssigns the ticket and returns the chosen agent ID, or returns the empty string when no agent is eligible.
resolveTicket(ticketId: string)voidResolves one assigned ticket.

Examples

OperationResult
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.
  • assignTicket is called only for an existing open ticket.
  • resolveTicket is 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.