A practice prompt we wrote. No company or candidate report names it, so it carries no company tag.
How to answer
The heap is easy. The grade is ordering, tie-breaks, and tickets that change while waiting.
- Pin the events.
add_ticket(t),agent_ready(a),close(ticket_id), andassign() -> list[(agent, ticket)]after each. Ask whether agents hold one ticket or several up to a cap. - Say the ordering rule out loud. “Priority first, then earliest deadline, then arrival order”: arrival is the fair tie-break. Ask: does a low-priority ticket near breach outrank a high-priority one with days left? Strict priority and earliest deadline fix the key at push time: one heap. Promotion near a breach is not: its key changes with the clock, and a heap never re-sorts. Use two heaps, by priority and by deadline: take the deadline top if inside the window, else the priority top.
- Say what fair means for agents. Among agents under their cap: fewest open tickets, then idle longest, then lowest id, so runs repeat. Load changes on every close, so skip stale entries here too.
- Build keys so ties never compare tickets. Push
(priority_rank, deadline, seq, ticket_id), withseqfrom a counter and rank 0 most urgent, becauseheapqpops the smallest. A missing deadline isdatetime.max, neverNone, which raises on the first tie. If your deadlines carry a time zone, usedatetime.max.replace(tzinfo=timezone.utc), because a naive and an aware datetime raise on comparison too. Withoutseq, ties compare objects and raise, or compare ids and lose arrival order. The heapq notes cover the counter and step 5’s removed-entry marks. - Handle changes with lazy deletion. Closed, assigned and reprioritized tickets stay in the heaps; map each id to its current entry, skip stale ones on pop, and rebuild once they outnumber live ones.
- Test the rule, not the heap. Equal tickets leave in arrival order, a reprioritized one jumps, a closed one never leaves, all under a fixed clock.
The trap is strict priority with no word about starvation: name it and offer aging.
Follow-ups
What the interviewer may ask next, once your first answer is on the table.
- An agent goes offline holding three tickets. What happens to them, and do they keep their place in line?
- The support lead asks why one ticket waited all afternoon. Can your system answer from its own records?
- Agents have skills, and a billing ticket can only go to a billing agent. What changes in your data structures?
- Under steady high-priority load, low-priority tickets never get picked. Is that acceptable, and what would you add?
Where answers go wrong
- Pushing tuples that end in a ticket object, which raises a TypeError the first time two tickets tie on priority and deadline.
- Putting a time-dependent key, such as promotion near a breach, into a single heap, so the order is right when a ticket is pushed and wrong once the clock moves.
- Calling the tie-break “fair” without saying what fair means, for tickets or for agents.
Answer this in two minutes
Write the answer you would say out loud. The clock starts with your first word.
Model answer
“Let me pin the events first: add_ticket, update_ticket for a changed priority or deadline, close, agent_ready and agent_offline, and assign(), which returns the new (agent, ticket) pairs. Agents hold several tickets up to a cap, and you said a support lead can change a ticket’s priority while it waits.