Minimum workers with first-free job assignment
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Anthropic's coding round and at Waymo. It tests heap, intervals and scheduling.
Jobs arrive as (job_id, start, end) with half-open times [start, end). Workers are numbered 0, 1, 2, ...
Part 1. Process jobs by start time (ties by job id). Each job goes to the lowest-numbered worker that is free at its start; hire a new worker only when every existing one is busy. Return the number of workers used and the worker of each job.
Part 2. Now there are exactly k workers. Jobs are served first in, first out: a job starts at the later of its release time, the previous job's start, and the moment a worker is free (again the lowest-numbered one among those free then). Each job keeps its duration. Return each job's worker, start and finish.
Example: jobs a [0, 10), b [2, 5), c [5, 12), d [10, 11) need 2 workers: a → 0, b → 1, c → 1, d → 0. With k = 1, b runs [10, 13), c [13, 20) and d [20, 21).
def assign_workers(jobs):
"""-> (workers needed, {job_id: worker})"""
def schedule_with_pool(jobs, k):
"""-> {job_id: (worker, start, finish)}"""
Clarifying questions
- With half-open intervals, a worker whose job ends at
tis free for a job starting att; confirm. - Can
start == end(zero-length jobs), and how should they be handled? - In Part 2, is the release time the job's
start, and its durationend - start? - In Part 2, if several workers become free at once, is it again the lowest-numbered one among them?
- How many jobs: does
O(n log n)suffice?
What a strong answer covers
- Part 1: sort by
(start, job_id), keep a min-heap of busy workers keyed by end time and a min-heap of free worker ids; before each job, move every worker withend <= startto the free heap, then take the smallest free id or hire the next one.O(n log n). - Why a single heap by end time is not enough: the rule picks the lowest-numbered free worker, not the one that freed first.
- Part 2: process jobs in FIFO order; a job's start is the maximum of its release, the previous job's start, and the earliest moment any of the
kworkers is free; then free every worker done by that moment and take the lowest id. - Checking the example by hand, including
creusing worker 1 at time 5 anddreusing worker 0 at time 10. - Tests: identical start times, jobs that touch end to start,
k = 1,klarger than needed, and an empty job list.
Follow-ups
- Return the peak number of jobs running at once without assigning workers.
- Jobs arrive online: assign each as it arrives.
- Workers have different speeds or costs; minimize total cost.
- Part 2 with priorities instead of FIFO.
Where it's asked
- Anthropic · Coding55 min · Live coding on a realistic, multi-part problem, a different one from the screen · No AI
- Waymo
What it tests
heap · intervals · scheduling
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

