KatInterview

Assign Support Tickets to Agents Within Concurrency Limits

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalsgreedyinterval-schedulingheap

Last updated

Given a list of fixed-time support tickets, assign each to an agent while respecting per-client concurrency caps and an optional agent pool size.

Background

You are building the assignment engine for a support ticketing platform. Incoming tickets arrive with a fixed start time and a known handling duration. Your job is to map every ticket to a support agent so the platform can route work without conflicts.

Task

Implement the function:

def assign_tickets(tickets, max_concurrency, num_agents=None):
    ...

It should return a dictionary mapping each ticket_id to an agent_id (agents are numbered from 0). If it is impossible to satisfy all constraints, return an empty dictionary {}.

Inputs

  • tickets: a list of dictionaries, each with:
    • ticket_id (int) — unique identifier for the ticket
    • client_id (int) — the client who opened the ticket
    • start_time (int) — the hour at which handling begins
    • duration (int) — how many hours the ticket takes to resolve
  • max_concurrency (int) — the maximum number of tickets a single client may have in-flight at the same time
  • num_agents (int or None) — if given, the total number of available agents; if None, treat the pool as unbounded and use as few agents as possible

Rules

  1. An agent can handle at most one ticket at a time.
  2. A ticket must be assigned to an agent who is free for its entire duration.
  3. Intervals are half-open: a ticket covering [start, start + duration) ends before start + duration, so an agent finishing at hour 4 is free to take a ticket starting at hour 4.
  4. Start times are fixed — you are assigning tickets to agents, not rescheduling them.
  5. No client may have more than max_concurrency tickets active at the same instant.

Example

tickets = [
    {'ticket_id': 1, 'client_id': 42, 'start_time': 0, 'duration': 3},
    {'ticket_id': 2, 'client_id': 42, 'start_time': 2, 'duration': 2},
]

assign_tickets(tickets, max_concurrency=1)
# Tickets [0,3) and [2,4) overlap for client 42 → impossible
# returns {}

assign_tickets(tickets, max_concurrency=2)
# returns {1: 0, 2: 1}  (or any valid two-agent assignment)

Constraints

  • 0 <= start_time < 24
  • 1 <= duration <= 24
  • 1 <= len(tickets) <= 1000
  • All ticket_id values are unique
  • Time is measured in integer hourly blocks; no day wrap-around

Hints

Hint 1

Before touching agent assignment at all, check whether the client concurrency rule is even satisfiable. Because start times are fixed, this is a pure feasibility question you can answer with a sweep line over each client's intervals independently — no assignment decisions needed.

Hint 2

For the agent assignment itself, think of it as interval partitioning: sort tickets by start time, maintain a min-heap of (end_time, agent_id) for busy agents, and reclaim any agent whose end_time is ≤ the current ticket's start_time. The half-open rule means 'end ≤ start' is the right release condition, not 'end < start'.

Hint 3

To prove your greedy uses the fewest agents possible, ask: when does it open a new agent? Only when every existing agent is still busy. Count how many tickets overlap at that moment — that count is a lower bound on the number of agents any valid solution needs, so the greedy is optimal.