KatInterview

Route Jobs to Workers with Account Stickiness

Heaps, Sorting & SearchAuto-graded · 19 test cases
MediumAuto-gradedHeaps, Sorting & Searchheapsimulationscheduling

Last updated

Simulate FIFO dispatch of tasks to workers with a preference for each account's last worker, using heaps and a lookup table to track free and busy workers.

Sticky Task Router

A processing platform has a fixed pool of workers and a queue of tasks. Every task is already in the queue at time 0, and tasks are dispatched strictly in the order given. Your job is to decide which worker handles each task and when it finishes.

Input

  • tasks: a list of dicts, each with integer keys task_id, account_id and processing_time.
  • workers: a list of dicts, each with an integer worker_id and a string type. Worker IDs are unique but not necessarily consecutive. The type field (for example "General" or "Specialized") is informational only and has no effect on the result.

Dispatch model

Keep a single dispatch clock that starts at 0. Each worker handles one task at a time. A worker that starts a task at clock value c with processing time p is busy until c + p. A worker is idle at clock value c if it has never been used, or if its busy-until time is less than or equal to c.

Handle the tasks one by one, in input order:

  1. If no worker is idle at the current clock, advance the clock to the smallest busy-until time among all workers. Every worker whose busy-until time is at most the new clock value is now idle. The clock changes only in this situation. It never moves forward otherwise, so several consecutive tasks can start at the same clock value.

  2. Choose a worker for the task:

    • Stickiness: if the task's account_id has been handled before, and the worker who handled that account most recently is idle right now, choose that worker.
    • Otherwise choose the idle worker with the smallest worker_id.

    Stickiness is only a preference. Never delay a task to wait for a busy preferred worker.

  3. The task starts at the current clock value, so its finish_time is clock + processing_time. The chosen worker is busy until that time. Record this worker as the latest handler of the task's account.

Output

Return a list with one dict per task, in the same order as the input tasks. Each dict has the keys task_id, worker_id (the chosen worker) and finish_time.

Constraints

  • 1 <= len(tasks) <= 10^5
  • 1 <= len(workers) <= 1000
  • Every processing_time is a positive integer.
  • Worker IDs are distinct.
  • Tasks must be dispatched in the given order, and the output order matches the input order.

Examples

Example 1

Input: route_tasks([{'task_id': 1, 'account_id': 100, 'processing_time': 5}, {'task_id': 2, 'account_id': 100, 'processing_time': 3}], [{'worker_id': 1, 'type': 'General'}, {'worker_id': 2, 'type': 'General'}])

Output: [{'task_id': 1, 'worker_id': 1, 'finish_time': 5}, {'task_id': 2, 'worker_id': 2, 'finish_time': 3}]

Example 2

Input: route_tasks([{'task_id': 1, 'account_id': 10, 'processing_time': 4}, {'task_id': 2, 'account_id': 20, 'processing_time': 2}, {'task_id': 3, 'account_id': 30, 'processing_time': 3}], [{'worker_id': 1, 'type': 'General'}, {'worker_id': 2, 'type': 'Specialized'}])

Output: [{'task_id': 1, 'worker_id': 1, 'finish_time': 4}, {'task_id': 2, 'worker_id': 2, 'finish_time': 2}, {'task_id': 3, 'worker_id': 2, 'finish_time': 5}]

Example 3

Input: route_tasks([{'task_id': 1, 'account_id': 1, 'processing_time': 5}, {'task_id': 2, 'account_id': 1, 'processing_time': 2}, {'task_id': 3, 'account_id': 2, 'processing_time': 1}, {'task_id': 4, 'account_id': 2, 'processing_time': 4}], [{'worker_id': 3, 'type': 'General'}, {'worker_id': 7, 'type': 'General'}])

Output: [{'task_id': 1, 'worker_id': 3, 'finish_time': 5}, {'task_id': 2, 'worker_id': 7, 'finish_time': 2}, {'task_id': 3, 'worker_id': 7, 'finish_time': 3}, {'task_id': 4, 'worker_id': 7, 'finish_time': 7}]

Hints

Hint 1

The clock only jumps when every worker is busy, and it jumps to the earliest busy-until time. What structure gives you that earliest time quickly?

Hint 2

Keep two heaps: one of idle worker IDs (to get the smallest ID) and one of (busy_until, worker_id) pairs. Before each task, move every worker with busy_until <= clock from the busy heap to the idle structure.

Hint 3

For stickiness, store the last worker per account in a dict and keep an is_idle flag per worker. If you take the preferred worker out of the middle of the idle heap, delete lazily: when popping, skip IDs whose flag says busy.