Parallel Map with a Thread Pool
medium · heap scheduling
Simulate a thread pool using a fake clock. Given tasks (integers), durations (nonnegative integers of the same length), workers >= 1, and multiplier, assign tasks in input order to the worker with the smallest (available_time, worker_id). Workers start available at time 0 and are numbered from 0. Each task starts at that worker's availability and finishes duration later. Return {'results': mapped task values in INPUT order, 'completion': task indices sorted by (finish_time, task_index)}. A mapped value is task * multiplier. Zero durations are allowed, so the same worker can receive several zero-duration tasks. Use only deterministic scheduling, no threads or sleeps.
Examples (JSON; null means Python None):
Input: {"tasks": [2, 3, 4], "durations": [5, 1, 1], "workers": 2, "multiplier": 2}
Output: {"results": [4, 6, 8], "completion": [1, 2, 0]}
Input: {"tasks": [3, 1], "durations": [2, 2], "workers": 2, "multiplier": -1}
Output: {"results": [-3, -1], "completion": [0, 1]}
Starter code
def parallel_map(tasks: list[int], durations: list[int], workers: int, multiplier: int) -> dict:
"""Model ordered map results and deterministic out-of-order completion."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler