KatInterview

Skill-Based Task Dispatcher Simulation

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesignsimulationheap

Last updated

Simulate a dispatcher that gives queued tasks to skilled workers by project priority and current load, using per-project queues and tie-break rules.

Skill-Based Task Dispatcher

You are building a small dispatcher that hands queued work items to workers. Each worker has a set of skills and a limit on how many items they can hold at once. Each project demands certain skills and has a priority.

Instead of a class, you implement one function that replays a script of commands against a fresh, empty dispatcher and returns one result per command.

Input

  • commands: a list of command names.
  • payloads: a list of the same length. payloads[i] is the argument list for commands[i].

Commands

CommandPayloadEffectResult recorded
"register_annotator"[worker_id, skills, capacity]Creates a worker, or updates an existing worker's skills and capacity. A worker that already exists keeps all of its currently active tasks.None
"add_project"[project_id, required_skills, priority]Creates a project, or updates an existing project's required skills and priority. An existing project keeps its pending tasks and its original creation order.None
"add_tasks"[project_id, task_ids]Appends the task IDs, in the given order, to the end of that project's pending queue.None
"assign_next"[]Performs an assignment round (see below).List of [task_id, project_id, worker_id] triples, in the order the assignments were made. The list is empty if nothing was assigned.
"complete_task"[task_id]Finishes an active task and frees one slot for the worker holding it.True if the task was active, otherwise False (unknown, still pending, or already completed)

Assignment round

During "assign_next":

  1. Consider only projects that currently have at least one pending task.
  2. Visit those projects from highest priority to lowest. Projects with equal priority are visited in the order they were first created.
  3. For the current project, repeatedly take the task at the front of its queue (FIFO) and give it to a worker who is eligible:
    • The worker has every skill in the project's required_skills. An empty requirement list is satisfied by every worker.
    • The worker's number of active tasks is strictly less than their capacity. A worker whose capacity is 0, or was lowered below their current load, is not eligible.
  4. If several workers are eligible, pick the one with the fewest active tasks. If that is still tied, pick the one whose ID is lexicographically smallest.
  5. Assigning a task immediately raises that worker's active count by one, so later assignments in the same round see the updated load.
  6. If no worker is eligible for a project, leave its remaining tasks pending and move on to the next project. A blocked project never prevents lower-priority projects from being served. Never discard a task that could not be assigned.

Assigned tasks stay active until "complete_task" is called for them. Task IDs are unique across the whole script. A task is never assigned twice.

Output

Return a list with exactly one entry per command, in order, following the "Result recorded" column above.

Constraints

  • Total tasks <= 10^5.
  • Workers <= 500.
  • Projects <= 1,000.
  • Each skill list has at most 20 entries. Skill lists may contain duplicates, and order does not matter.
  • All IDs are non-empty alphanumeric strings.
  • "add_tasks" is only issued for projects that already exist.

Examples

Example 1

Input: simulate_dispatcher(['register_annotator', 'register_annotator', 'add_project', 'add_tasks', 'add_project', 'add_tasks', 'assign_next', 'complete_task', 'complete_task'], [['ann_1', ['nlp', 'audio'], 2], ['ann_2', ['nlp'], 1], ['proj_a', ['nlp'], 5], ['proj_a', ['t1', 't2']], ['proj_b', ['audio'], 10], ['proj_b', ['t3']], [], ['t1'], ['non_existent']])

Output: [None, None, None, None, None, None, [['t3', 'proj_b', 'ann_1'], ['t1', 'proj_a', 'ann_2'], ['t2', 'proj_a', 'ann_1']], True, False]

Hints

Hint 1

Keep pending state (one FIFO queue per project) separate from active state (task to worker, and worker to current load).

Hint 2

Treat skills as sets and test with a subset check. Use a strict 'load < capacity' check so lowered capacities never produce negative free slots.

Hint 3

Inside a project, build a min-heap of eligible workers keyed by (load, id), pop the best, assign, then push the worker back only if they still have room. Rebuild the heap for each project so loads from earlier projects are respected.