{"id":"ood-call-center","title":"Design a call center","sections":[{"id":"overview","title":"Overview","html":"<p>Picture a support line with three levels of staff. Operators answer the phone. When an operator can't solve a caller's problem, they hand the call to a supervisor. When a supervisor can't solve it either, it goes to a director, and a director can solve anything. On a busy morning every phone is taken, so new callers wait on hold until someone frees up. The question asks you to model that in code: who takes a new call, what happens when a call is passed up, and who picks up the callers on hold.</p>\n<p>This is an <strong>object-oriented design question</strong>. Object-oriented design (OOD) means describing a program as a set of objects, each holding its own data and offering a few operations on it. In the interview you clarify the scope with a few questions, name the classes and what each one is responsible for, write the code, and then discuss how the design would change if the requirements grew. There are no servers here: the interviewer judges whether the classes are easy to understand, easy to change and correct.</p>","headings":[]},{"id":"constraints-and-assumptions","title":"Constraints and assumptions","html":"<h3 id=\"constraints-and-assumptions\">Constraints and assumptions</h3>\n<p>Ask these questions before writing any code. Each answer is what we assume for the rest of the chapter.</p>\n<ul>\n<li><strong>What levels of employees are in the call center?</strong> Operator, supervisor and director. This fixes the number of ranks, and so the number of employee classes.</li>\n<li><strong>Can we assume operators always get the initial calls?</strong> Yes. Every new call starts at the lowest rank, so routing always begins in the same place.</li>\n<li><strong>If no operator is available, or the operator can't handle the call, does the call go to the supervisors?</strong> Yes. This gives two different reasons a call moves up: everyone at a rank is busy (overflow), or the person on the line can't help (escalation). The code has to handle both.</li>\n<li><strong>If no supervisor is available, or the supervisor can't handle the call, does the call go to the directors?</strong> Yes. The same two rules apply one level higher.</li>\n<li><strong>Can we assume the directors can handle all calls?</strong> Yes. The chain of escalation has an end, so a call can never be passed up forever.</li>\n<li><strong>What happens if nobody can answer the call?</strong> It gets queued. We need a waiting line and a rule for who takes the next waiting call when someone frees up.</li>\n<li><strong>Do we need to handle \"VIP\" calls, where we put someone at the front of the line?</strong> No. The waiting line is first in, first out, which keeps the queue simple. Priorities come back as an extension.</li>\n<li><strong>Can we assume inputs are valid, or do we have to validate them?</strong> Assume they're valid. We skip checks such as \"is this a real employee ID?\" and keep the code focused on routing.</li>\n</ul>\n<p>Three smaller assumptions fill gaps the questions leave open:</p>\n<ul>\n<li>An employee handles one call at a time.</li>\n<li>A call that has been escalated may only go to the rank it was escalated to or higher. An operator can't take it back.</li>\n<li>When an employee frees up, they take the waiting call that most needs someone of their rank. The <a href=\"/learn/ood-call-center#class-design\">Class design</a> section explains the rule.</li>\n</ul>","headings":[{"id":"constraints-and-assumptions","title":"Constraints and assumptions","depth":3}]},{"id":"class-design","title":"Class design","html":"<h3 id=\"class-design\">Class design</h3>\n<p>Start with the nouns in the problem: employee, operator, supervisor, director, call, call center. Most of them become classes. Then add the two fixed lists of values the problem mentions: the ranks and the states a call can be in.</p>\n<figure class=\"diagram\" data-text=\"CallCenter -> Call (queues waiting calls); CallCenter -> Employee (abstract) (staff, by rank); Employee (abstract) -> Call (handles one); Call -> CallState (enum); Operator -> Employee (abstract) (is an); Supervisor -> Employee (abstract) (is an); Director -> Employee (abstract) (is an); Employee (abstract) -> Rank (enum)\"><div class=\"diagram-scroll\"><svg class=\"dg\" viewBox=\"0 0 640 318\" width=\"640\" role=\"img\" aria-label=\"Classes in the call center. Solid lines show which class holds or uses which.\"><defs><marker id=\"dg-arrow\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path class=\"dg-arrowhead\" d=\"M0,1 L9,5 L0,9 z\"></path></marker></defs><line class=\"dg-edge\" x1=\"311\" y1=\"42\" x2=\"487\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"240\" y1=\"69\" x2=\"240\" y2=\"131\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"311\" y1=\"135.4\" x2=\"487\" y2=\"69.4\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"560\" y1=\"69\" x2=\"560\" y2=\"131\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"116\" y1=\"255\" x2=\"198.7\" y2=\"193\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"240\" y1=\"255\" x2=\"240\" y2=\"193\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"364\" y1=\"255\" x2=\"281.3\" y2=\"193\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"311\" y1=\"188.6\" x2=\"487\" y2=\"254.6\" marker-end=\"url(#dg-arrow)\"></line><rect class=\"dg-node dg-box\" x=\"172\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"46.5\">CallCenter</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"46.5\">Call</tspan></text><rect class=\"dg-node dg-box\" x=\"172\" y=\"136\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"158.5\">Employee</tspan><tspan x=\"240\" y=\"174.5\">(abstract)</tspan></text><rect class=\"dg-node dg-note\" x=\"492\" y=\"136\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label dg-muted\" text-anchor=\"middle\"><tspan x=\"560\" y=\"158.5\">CallState</tspan><tspan x=\"560\" y=\"174.5\">(enum)</tspan></text><rect class=\"dg-node dg-box\" x=\"12\" y=\"258\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"80\" y=\"286.5\">Operator</tspan></text><rect class=\"dg-node dg-box\" x=\"172\" y=\"258\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"286.5\">Supervisor</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"258\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"286.5\">Director</tspan></text><rect class=\"dg-node dg-note\" x=\"492\" y=\"258\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label dg-muted\" text-anchor=\"middle\"><tspan x=\"560\" y=\"286.5\">Rank (enum)</tspan></text><rect class=\"dg-edge-bg\" x=\"332\" y=\"33\" width=\"134\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"399\" y=\"46\" text-anchor=\"middle\">queues waiting calls</text><rect class=\"dg-edge-bg\" x=\"191.6\" y=\"91\" width=\"96.8\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"240\" y=\"104\" text-anchor=\"middle\">staff, by rank</text><rect class=\"dg-edge-bg\" x=\"359.9\" y=\"93.4\" width=\"78.2\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"399\" y=\"106.4\" text-anchor=\"middle\">handles one</text><rect class=\"dg-edge-bg\" x=\"136.8\" y=\"215\" width=\"41\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"157.3\" y=\"228\" text-anchor=\"middle\">is an</text><rect class=\"dg-edge-bg\" x=\"219.5\" y=\"215\" width=\"41\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"240\" y=\"228\" text-anchor=\"middle\">is an</text><rect class=\"dg-edge-bg\" x=\"302.2\" y=\"215\" width=\"41\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"322.7\" y=\"228\" text-anchor=\"middle\">is an</text></svg></div><figcaption>Classes in the call center. Solid lines show which class holds or uses which.</figcaption></figure>\n<ul>\n<li><strong><code>Rank</code></strong> is an enum, a type with a fixed set of named values: <code>OPERATOR</code>, <code>SUPERVISOR</code> and <code>DIRECTOR</code>. We make it an <code>IntEnum</code>, whose members are also integers 0, 1 and 2, so the code can write <code>rank >= call.min_rank</code> and loop over ranks from lowest to highest.</li>\n<li><strong><code>CallState</code></strong> is an enum of where a call is in its life: <code>READY</code> (just created or just escalated), <code>QUEUED</code> (on hold), <code>IN_PROGRESS</code> (someone is talking to the caller) and <code>COMPLETE</code>.</li>\n<li><strong><code>Call</code></strong> holds one caller's data: an ID, the caller, its state, the employee on the line, and <code>min_rank</code>, the lowest rank allowed to take it. Escalation raises <code>min_rank</code>. It also keeps <code>handled_by</code>, the names of everyone who has taken the call, which is handy for reporting and for the walkthrough.</li>\n<li><strong><code>Employee</code></strong> is an <strong>abstract base class</strong>: a class that defines what every employee can do but can't be created on its own. It knows how to take, complete and escalate a call. The one thing it leaves open is <code>escalation_rank()</code>, the rank a call goes to when this employee passes it on. Python's <code>abc</code> module enforces this: <code>Employee(1, \"Ana\")</code> raises a <code>TypeError</code>, because a method marked <code>@abstractmethod</code> has no body.</li>\n<li><strong><code>Operator</code>, <code>Supervisor</code> and <code>Director</code></strong> are subclasses of <code>Employee</code>. This is <strong>inheritance</strong>: each one \"is an\" employee and gets all of the shared behavior for free. Each sets its <code>rank</code> and answers <code>escalation_rank()</code>. An operator escalates to supervisors, a supervisor to directors, and a director refuses with a <code>CannotEscalate</code> error.</li>\n<li><strong><code>CallCenter</code></strong> owns the staff and the waiting calls, grouped by rank. This is <strong>composition</strong>: the call center \"has\" employees and calls, and it's the only class that decides who gets which call. It offers <code>dispatch_call()</code> for a call that needs someone and <code>employee_freed()</code> for an employee who has just become free.</li>\n</ul>\n<p>The division of work matters. An employee knows what <em>it</em> can do, and the call center knows about <em>everyone</em>. When an operator escalates, it doesn't search for a supervisor itself. It raises the call's <code>min_rank</code> and asks the call center to place the call. That keeps the search logic in one place.</p>\n<h4 id=\"who-takes-a-waiting-call\">Who takes a waiting call</h4>\n<p>The waiting calls live in one first-in-first-out queue per rank. A queue is a line where items leave in the order they arrived. A call waits in the queue for its <code>min_rank</code>. When an employee frees up, <code>employee_freed()</code> checks the queues they're allowed to serve, starting with the most senior. A free director first takes calls escalated to directors, then calls escalated to supervisors, then ordinary calls. An operator can only take ordinary calls.</p>\n<p>The reason is scarcity. Only directors can take a director-level call, so a free director should take one if any is waiting. Ordinary calls can be served by anyone, so they can wait for whoever frees up next.</p>","headings":[{"id":"class-design","title":"Class design","depth":3},{"id":"who-takes-a-waiting-call","title":"Who takes a waiting call","depth":4}]},{"id":"solution","title":"Solution","html":"<h3 id=\"solution\">Solution</h3>\n<p>Here is the complete program. It uses only the standard library and runs on Python 3.9 or later.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __future__</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> annotations</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> abc </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> ABC</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, abstractmethod</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> collections </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> deque</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> enum </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Enum, IntEnum</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Rank</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">IntEnum</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Employee ranks, lowest first. IntEnum lets us compare ranks with &#x3C; and >=.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    OPERATOR</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    SUPERVISOR</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    DIRECTOR</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 2</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> CallState</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Enum</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    READY</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> \"ready\"</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">              # created or just escalated, not yet placed</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    QUEUED</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> \"queued\"</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">            # waiting because nobody eligible is free</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    IN_PROGRESS</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> \"in progress\"</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # an employee is on the line</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    COMPLETE</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> \"complete\"</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">        # finished</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> CannotEscalate</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">Exception</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Raised when the highest rank tries to pass a call on.\"\"\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Call</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, call_id: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, caller: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call_id</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.caller </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> caller</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.min_rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">OPERATOR</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">   # lowest rank allowed to take this call</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.state </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallState.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">READY</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.employee: Employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.handled_by: list[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># everyone who has taken the call</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __repr__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Call(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_id</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.state.value</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">, min_rank=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.min_rank.name</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">)\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Employee</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">ABC</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    rank: Rank  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># each concrete subclass sets this</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, employee_id: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, name: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.employee_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee_id</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.name </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> name</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call: Call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_center: CallCenter </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> =</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">  # set when the center hires them</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">    @</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">property</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> is_free</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">bool</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> take_call</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, call: Call) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.state </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallState.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">IN_PROGRESS</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.handled_by.append(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.name)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> complete_call</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Call:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._hang_up()</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.state </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallState.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">COMPLETE</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_center.employee_freed(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> escalate_call</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Call:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        target </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.escalation_rank()     </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># raises for a director, before anything changes</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._hang_up()</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.min_rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> target</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.state </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallState.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">READY</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_center.dispatch_call(call)  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># find someone senior first</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call_center.employee_freed(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># then this employee picks up new work</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">    @abstractmethod</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> escalation_rank</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Rank:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"The rank a call goes to when this employee can't handle it.\"\"\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> _hang_up</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Call:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            raise</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> RuntimeError</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.name</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> has no call\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.call, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __repr__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{type</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">).</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">__name__}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.name</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">)\"</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Operator</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Employee</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">OPERATOR</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> escalation_rank</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Rank:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">SUPERVISOR</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Supervisor</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Employee</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">SUPERVISOR</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> escalation_rank</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Rank:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">DIRECTOR</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Director</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">Employee</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">DIRECTOR</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> escalation_rank</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> Rank:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        raise</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CannotEscalate(</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Directors handle every call\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> CallCenter</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, employees: list[Employee]) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.staff: dict[Rank, list[Employee]] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {rank: [] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank}</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.waiting: dict[Rank, deque[Call]] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {rank: deque() </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank}</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employees:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">            self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.hire(employee)</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> hire</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, employee: Employee) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        employee.call_center </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.staff[employee.rank].append(employee)</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.employee_freed(employee)  </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># a new hire can take a waiting call at once</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> dispatch_call</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, call: Call) -> Employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"Give the call to the lowest eligible rank with someone free, else queue it.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Rank:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call.min_rank:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                continue</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._first_free(rank)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                employee.take_call(call)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        call.state </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallState.</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">QUEUED</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.waiting[call.min_rank].append(call)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> employee_freed</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, employee: Employee) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"Hand a newly free employee the most senior call they are allowed to take.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee.is_free:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            return</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> sorted</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(Rank, </span><span style=\"color:#E36209;--shiki-dark:#FFAB70\">reverse</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">True</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee.rank </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">and</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.waiting[rank]:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                employee.take_call(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.waiting[rank].popleft())</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> queued_calls</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self) -> </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> sum</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(q) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> q </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.waiting.values())</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> _first_free</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, rank: Rank) -> Employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">|</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.staff[rank]:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee.is_free:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> employee</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span></code></pre>\n<p>The key parts:</p>\n<ul>\n<li><strong>Routing a call.</strong> <code>dispatch_call()</code> walks the ranks from lowest to highest, skipping any below the call's <code>min_rank</code>. It gives the call to the first free employee it finds. A new call therefore goes to an operator when one is free, then spills over to a supervisor, then a director. If nobody eligible is free, the call is marked <code>QUEUED</code> and joins the queue for its <code>min_rank</code>.</li>\n<li><strong>Escalating.</strong> <code>escalate_call()</code> first asks <code>escalation_rank()</code> where the call should go. For a director that raises <code>CannotEscalate</code> before anything has changed, so the call stays with the director. Otherwise the employee hangs up, the call's <code>min_rank</code> goes up, and the call center places it again. Only after that does the employee look for new work. That order matters: the call has already left, so the employee can't pick it straight back up from the queue.</li>\n<li><strong>Completing.</strong> <code>complete_call()</code> hangs up, marks the call <code>COMPLETE</code> and tells the call center that the employee is free. The call center then gives them the most senior waiting call they're allowed to take, if any.</li>\n<li><strong>Hiring.</strong> <code>hire()</code> links the employee to the call center and immediately offers them a waiting call. A new hire on a busy morning starts working at once.</li>\n<li><strong>Shared code in the base class.</strong> <code>take_call()</code>, <code>complete_call()</code>, <code>escalate_call()</code> and <code>_hang_up()</code> are written once in <code>Employee</code>. Each subclass is only a few lines long. Adding a fourth rank such as \"manager\" means one new enum value and one new subclass.</li>\n</ul>\n<h4 id=\"mistakes-this-code-avoids\">Mistakes this code avoids</h4>\n<p>Code for this question often has the right shape and still fails when you run it. These are the usual traps, and how this version avoids each one:</p>\n<ul>\n<li><strong>Constructors that need the call center.</strong> If every employee needs the call center passed to its constructor, it's easy to forget an argument or name the wrong class in <code>super()</code>, and then no employee can be created. Here <code>hire()</code> sets the call center, so employees can be created first.</li>\n<li><strong>Two names for one idea.</strong> If escalation sets an attribute called <code>level</code> while routing reads one called <code>rank</code>, an escalated call is routed as if it was never escalated. Here both use <code>min_rank</code>.</li>\n<li><strong>Employees who never become free.</strong> If completing a call doesn't clear the employee's current call, every employee stays busy after their first call. <code>_hang_up()</code> clears it.</li>\n<li><strong>Empty methods.</strong> The steps for escalated, completed and queued calls are the heart of the design, and they're the easiest to leave as stubs. Here they're implemented as <code>dispatch_call()</code> and <code>employee_freed()</code>.</li>\n<li><strong>One shared queue for every rank.</strong> With one queue, a freed operator can pull out a call that needs a director. Here there's one queue per rank, and an employee only takes calls at or below their rank.</li>\n<li><strong><code>raise NotImplemented(...)</code>.</strong> <code>NotImplemented</code> is a special value, not an exception class, so that line fails with a <code>TypeError</code>. Here the director's refusal raises a dedicated <code>CannotEscalate</code> exception.</li>\n<li><strong>A call on hold that looks like any other.</strong> A <code>QUEUED</code> state tells a waiting call apart from one that was just escalated and is about to be placed.</li>\n</ul>","headings":[{"id":"solution","title":"Solution","depth":3},{"id":"mistakes-this-code-avoids","title":"Mistakes this code avoids","depth":4}]},{"id":"walking-through-an-example","title":"Walking through an example","html":"<h3 id=\"walking-through-an-example\">Walking through an example</h3>\n<p>Take a small team: two operators (Ana and Ben), one supervisor (Sam) and one director (Dee). Five calls arrive one after another.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">ana, ben </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Operator(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Ana\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">), Operator(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">2</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Ben\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">sam </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Supervisor(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">3</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Sam\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">dee </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> Director(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">4</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"Dee\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">center </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> CallCenter([ana, ben, sam, dee])</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">calls </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [Call(i, </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">f</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"caller-</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">{</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">i</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">}</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> i </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> range</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">6</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)]</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> call </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> calls:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    print</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(call.call_id, center.dispatch_call(call))</span></span></code></pre>\n<ol>\n<li>Call 1 goes to Ana and call 2 to Ben, the two operators.</li>\n<li>Call 3 is an ordinary call, but both operators are busy, so it spills over to Sam.</li>\n<li>Call 4 spills over again, to Dee.</li>\n<li>Call 5 finds everyone busy. <code>dispatch_call()</code> returns <code>None</code> and the call waits in the operator queue with state <code>QUEUED</code>.</li>\n</ol>\n<p>Now the calls start to move.</p>\n<ol start=\"5\">\n<li>Ana can't solve call 1 and calls <code>ana.escalate_call()</code>. The call's <code>min_rank</code> becomes <code>SUPERVISOR</code>. Sam and Dee are both busy, so call 1 waits in the supervisor queue. Ana is now free, and <code>employee_freed(ana)</code> hands her call 5 from the operator queue. The caller on hold is finally answered.</li>\n<li>Sam finishes call 3 with <code>sam.complete_call()</code>. Sam checks the supervisor queue first and finds call 1, so call 1 is now in progress with Sam.</li>\n<li>Sam can't solve call 1 either and escalates it. Its <code>min_rank</code> becomes <code>DIRECTOR</code>. Dee is busy, so call 1 waits in the director queue. Sam checks the supervisor and operator queues, finds both empty, and stays free.</li>\n<li>Dee finishes call 4. The director queue holds call 1, so Dee takes it.</li>\n<li>Dee completes call 1. Its <code>handled_by</code> list reads <code>['Ana', 'Sam', 'Dee']</code>, and <code>center.queued_calls()</code> is 0.</li>\n</ol>\n<p>If Dee tried <code>dee.escalate_call()</code> in step 9, the call would raise <code>CannotEscalate</code> and the call would stay with Dee, unchanged.</p>\n<p>Notice step 7. Sam, the only supervisor, was busy with call 3, an ordinary call that had spilled over from the operators, while call 1 waited for a supervisor. Overflow made the escalated caller wait longer. The next section discusses that trade-off.</p>","headings":[{"id":"walking-through-an-example","title":"Walking through an example","depth":3}]},{"id":"design-choices-and-trade-offs","title":"Design choices and trade-offs","html":"<h3 id=\"design-choices-and-trade-offs\">Design choices and trade-offs</h3>\n<p><strong>Escalation: a chain of responsibility or a dispatcher.</strong> Chain of responsibility is a design pattern in which a request is passed along a chain of handlers, and each handler either deals with it or passes it to the next one. Escalation looks like that chain: operator, then supervisor, then director. A literal version would give each employee a link to the next employee. This design keeps the chain as data instead. Each employee class only names the next <em>rank</em>, and a central dispatcher, the <code>CallCenter</code>, picks which person at that rank gets the call. Links between individual people break as soon as that person is busy, on leave or replaced. A dispatcher can pick any free person, queue the call, and track waiting time in one place. The cost is that <code>CallCenter</code> becomes the class that knows most things, so keep it focused on routing.</p>\n<p><strong>Inheritance for ranks or a rank field.</strong> We used a subclass per rank. A simpler design would have one <code>Employee</code> class with a <code>rank</code> field and an <code>escalation_rank</code> looked up from a table. With only three ranks that differ in a single rule, both are fine, and the field version is shorter. Subclasses earn their place when the ranks start to behave differently. Directors might approve refunds, and supervisors might be able to listen in on calls. Interviewers usually want to see that you know both options and why you picked one. A good answer is \"subclasses, because behavior will diverge; a field if it's only data.\"</p>\n<p><strong>Overflow to senior staff.</strong> Sending ordinary calls up to supervisors when operators are busy answers callers sooner. As step 7 showed, it can also tie up the only people who can take escalated calls. A real center might keep one supervisor out of the overflow pool, or let senior staff take overflow only when their own queue is empty. Our <code>employee_freed()</code> already prefers senior work when someone frees up. A stricter rule would also stop <code>dispatch_call()</code> from spilling over while any escalated call is waiting.</p>\n<p><strong>Where escalated callers wait.</strong> An escalated call joins the back of its rank's queue. That's fair by the rule \"first in, first out,\" but the caller may already have waited once. Putting escalated calls at the front would be kinder, and it's a one-line change (<code>appendleft</code> instead of <code>append</code>). We kept plain first-in-first-out order because the constraints said there's no line-jumping.</p>\n<p><strong>Enums instead of strings or numbers.</strong> <code>Rank.SUPERVISOR</code> can't be misspelled the way the string <code>\"supervisr\"</code> can, and an editor can list every valid value. <code>IntEnum</code> adds ordering, which the routing loop relies on.</p>","headings":[{"id":"design-choices-and-trade-offs","title":"Design choices and trade-offs","depth":3}]},{"id":"extensions-and-follow-up-questions","title":"Extensions and follow-up questions","html":"<h3 id=\"extensions-and-follow-up-questions\">Extensions and follow-up questions</h3>\n<p><strong>Priorities.</strong> Suppose VIP callers now jump the line. Replace each rank's <code>deque</code> with a priority queue from <code>heapq</code>, keyed on (priority, arrival number). The arrival number breaks ties so that callers with equal priority stay in first-in-first-out order. Watch for starvation, where low-priority callers wait forever behind a steady stream of VIPs. A common fix is to raise a call's priority the longer it waits. <a href=\"/learn/queues-and-async#priorities\">Queues and asynchronous work</a> covers priority queues in more depth.</p>\n<p><strong>Skills-based routing.</strong> Real centers route by skill as well as rank: billing questions to billing staff, Spanish speakers to Spanish-speaking staff. Give each employee a set of skills and each call a set of required skills. <code>dispatch_call()</code> then looks for a free employee whose skills cover the call's. The waiting lines can no longer be one per rank. One option is a queue per combination of skills that actually occurs, and another is to scan a single queue for the oldest call the freed employee can handle.</p>\n<p><strong>Concurrency.</strong> Our code assumes calls arrive one at a time. In a real phone system, events arrive on many threads at once. Two threads could both see Ben as free and give him two calls. The simplest fix is a single <code>threading.Lock</code> held inside <code>dispatch_call()</code> and <code>employee_freed()</code>, so only one routing decision happens at a time. Routing is fast compared with a phone call, so one lock is rarely the bottleneck. Another option is to run the call center as one event loop that handles one event at a time.</p>\n<p><strong>Persistence.</strong> If the process restarts, every queued call disappears. Store calls and their states in a database, write a row whenever a call changes state, and rebuild the queues from the <code>QUEUED</code> rows on start-up. The same rows give you reporting for free: average waiting time, how often each rank escalates, and how long calls last.</p>\n<p><strong>Abandoned calls.</strong> Callers hang up while on hold. Add an <code>ABANDONED</code> state and a <code>cancel(call)</code> method that removes the call from its queue. Removing from the middle of a <code>deque</code> is slow for long queues, so a common trick is to mark the call cancelled and skip it when it reaches the front.</p>\n<p><strong>Making it a distributed system.</strong> A national support line has thousands of agents in many offices, so one in-memory <code>CallCenter</code> can't hold them all. The queues move out of the program into a message queue or a shared store, and each rank or skill gets its own queue. Agents' desktops pull calls from the queues they are allowed to serve, which is the same rule <code>employee_freed()</code> follows. The telephony system pushes new calls in. You then have to handle questions the single program never faced:</p>\n<ul>\n<li>What happens to a call if the agent's desktop crashes after taking it? The queue should only delete a call once the agent confirms it, and redeliver it after a timeout.</li>\n<li>How does each agent's presence (\"free\", \"busy\", \"on break\") reach the router quickly? Usually through heartbeats, small \"I'm still here\" messages sent every few seconds.</li>\n<li>How do you measure waiting time across all the queues?</li>\n</ul>\n<p><a href=\"/learn/queues-and-async\">Queues and asynchronous work</a> explains acknowledgements, redelivery and back pressure, the tools for those questions. For more practice in the same style, try the <a href=\"/learn/ood-deck-of-cards\">deck of cards</a> and <a href=\"/learn/ood-parking-lot\">parking lot</a> designs.</p>","headings":[{"id":"extensions-and-follow-up-questions","title":"Extensions and follow-up questions","depth":3}]},{"id":"check-yourself","title":"Check yourself","html":"<h3 id=\"check-yourself\">Check yourself</h3>\n<details>\n<summary>An operator escalates a call while every supervisor and director is busy. Trace what happens to the call and to the operator.</summary>\n<p>The call's <code>min_rank</code> becomes <code>SUPERVISOR</code> and <code>dispatch_call()</code> tries supervisors, then directors. Nobody is free, so the call is marked <code>QUEUED</code> and joins the supervisor queue. The operator is now free, so <code>employee_freed()</code> gives them the oldest ordinary call waiting in the operator queue, if there is one. The escalated call can't come back to the operator, because the operator's rank is below the call's <code>min_rank</code>.</p>\n</details>\n<details>\n<summary>Why does `escalate_call()` re-dispatch the call before it calls `employee_freed()` for the employee who escalated?</summary>\n<p>If the employee looked for work first, they would be free while the call was still unplaced. For an operator that's harmless, because the call needs a supervisor. But the order makes the rule obvious and safe for every rank: the escalated call is placed or queued first, and only then does the employee take new work. It also means the call gets first chance at any free senior employee before anything else changes.</p>\n</details>\n<details>\n<summary>A director, a supervisor-level call and an ordinary call are all waiting when the director frees up. Which call does the director take, and why?</summary>\n<p>The supervisor-level call, assuming no director-level call is waiting. <code>employee_freed()</code> checks queues from the most senior rank the employee can serve downward. Ordinary calls can be served by anyone who frees up later, but a supervisor-level call can only be served by supervisors or directors, which are scarcer. Serving the scarcest work first keeps the hardest calls from waiting the longest.</p>\n</details>\n<details>\n<summary>When would you replace the three employee subclasses with a single `Employee` class and a `rank` field?</summary>\n<p>When the ranks differ only in data. If the only difference is which rank comes next, a field and a small lookup table are shorter and just as clear. Subclasses are worth it when the ranks gain different behavior, such as directors approving refunds or supervisors monitoring calls. In that case each class keeps its own rules, and nothing else has to check the rank with <code>if</code> statements.</p>\n</details>\n<details>\n<summary>Two threads route calls at the same moment and both give a call to Ben. What went wrong, and what's the simplest fix?</summary>\n<p>Both threads read <code>ben.is_free</code> as true before either one called <code>take_call()</code>. This is a race condition: the result depends on the timing of the two threads. The simplest fix is one lock that every routing operation holds, so checking for a free employee and assigning the call happen as one step that no other thread can interrupt.</p>\n</details>\n<details>\n<summary>The call center grows to 5,000 agents in ten offices. What replaces the `waiting` dictionary, and what new failure do you have to handle?</summary>\n<p>A shared message queue or store, with a queue per rank or skill, that every office's router and agent desktops can reach. The new failure is a crash partway through: an agent's desktop takes a call and then dies. The queue should only delete the call after the agent confirms it is being handled, and redeliver it to someone else if no confirmation arrives in time.</p>\n</details>\n<h3 id=\"further-reading\">Further reading</h3>\n<ul>\n<li><a href=\"https://docs.python.org/3/library/abc.html\" target=\"_blank\" rel=\"noopener noreferrer\">abc: Abstract Base Classes</a>: Python's documentation for <code>ABC</code> and <code>@abstractmethod</code>, which make <code>Employee</code> impossible to create on its own.</li>\n<li><a href=\"https://docs.python.org/3/library/enum.html\" target=\"_blank\" rel=\"noopener noreferrer\">enum: Support for enumerations</a>: how <code>Enum</code> and <code>IntEnum</code> work, including why <code>IntEnum</code> members can be compared with <code>&#x3C;</code>.</li>\n<li><a href=\"https://refactoring.guru/design-patterns/chain-of-responsibility\" target=\"_blank\" rel=\"noopener noreferrer\">Chain of Responsibility (Refactoring.Guru)</a>: an illustrated explanation of the pattern escalation resembles, with its pros and cons.</li>\n<li><a href=\"https://docs.python.org/3/library/threading.html#lock-objects\" target=\"_blank\" rel=\"noopener noreferrer\">threading: Lock objects</a>: the lock you'd add to make routing safe with several threads.</li>\n<li><a href=\"/learn/queues-and-async\">Queues and asynchronous work</a>: the handbook chapter on the message queues a distributed call center would be built on.</li>\n</ul>","headings":[{"id":"check-yourself","title":"Check yourself","depth":3},{"id":"further-reading","title":"Further reading","depth":3}]}]}