KatInterview

Design a Support Agent Dispatcher with Interruption Handling

Data Structure DesignModel answer
MediumModel answerData Structure Designdesign-dsstacks-queuesheaps-sorting

Last updated

Design an in-memory dispatch engine that pairs queued support tickets with available agents, extended to handle mid-task agent breaks and fair ticket re-queuing.

Design an in-memory event-driven dispatch engine for a customer service team. The system routes incoming customer requests (tickets) to human agents, processes task completions, and manages agent availability state transitions.

Operational Requirements

Core Event Loop & Matching Engine

  1. Queues & Fairness:
    • Waiting tickets must be matched in strict FIFO order (earliest creation timestamp first).
    • Available agents must be assigned in FIFO order (earliest agent availability timestamp first).
  2. Deterministic Processing:
    • All events are processed sequentially in a single-threaded execution context.
    • Every event state change (e.g., ticket creation, agent sign-in, task completion) must immediately trigger an assignment check to match available tickets with available agents.

Event Handling Requirements

  • agent_sign_in(agent_id, timestamp): Registers an agent as free.
  • agent_sign_out(agent_id, timestamp): Removes an idle agent. If the agent is currently handling a ticket, reject or defer sign-out until idle.
  • customer_request_arrives(ticket_id, timestamp): Enqueues a new ticket.
  • task_completed(agent_id, ticket_id, timestamp): Marks the current assignment finished and sets the agent to available. Stale completion events (e.g., for a ticket no longer assigned to this agent) must be ignored safely.
  • agent_break_requested(agent_id, timestamp): Immediately interrupts the agent mid-task. The agent transitions to a BREAK state. The ticket being worked on must be re-queued immediately, preserving its original arrival timestamp so it retains priority over newer tickets.
  • agent_break_ended(agent_id, timestamp): Transitions the agent from BREAK back to available.

Clarifying Scope & Constraints

  • Assume all state fits in memory.
  • State transitions must handle invalid or stale events gracefully (e.g., duplicate sign-ins, operations on non-existent entities).
  • Be prepared to discuss the time complexity of each state update operation.