Task and worker assignment model
I was asked this
No reports yet. Sign in to say where you were asked it.
An object-oriented design interview question asked in Scale AI's coding round, last asked Sep 2022. It tests object-oriented design and state machines.
Design and implement Task and Worker classes and a small manager that drives them, then use it to print the system's state. One operator makes calls one after another in a single process. Tasks and workers are registered with IDs, and the operator chooses which task goes to which worker. A task starts PENDING, becomes ASSIGNED when given to a free worker, and becomes DONE when completed, which frees the worker for another task. Scheduling, timers, priorities, persistence and threads are out of scope.
Requirements
- Functional: register tasks and workers with non-empty, case-sensitive IDs, unique within their kind; assign a pending task to a free worker (a task can be assigned only once, a worker holds at most one task); complete an assigned task, which records the worker who did it and frees that worker; return a status summary.
- Errors: reject unknown IDs, duplicate registration, assigning a task that isn't pending, assigning to a busy worker and completing a task that isn't assigned. A rejected call changes nothing.
- Status: tasks sorted by ID with their state and worker (or none yet), then workers sorted by ID with their current task (or none). The summary is a value copy; callers can't change the system through it.
Unless you agree on a different interface with the interviewer, assume:
m = TaskManager()
m.add_task("t1"); m.add_task("t2")
m.add_worker("w1"); m.add_worker("w2")
m.assign("t1", "w1")
m.assign("t2", "w1") -> error: worker w1 is busy (state unchanged)
m.complete("t1")
m.assign("t2", "w1")
m.status()
# tasks: t1 DONE by w1 | t2 ASSIGNED to w1
# workers: w1 -> t2 | w2 -> none
Clarifying questions
- Should errors be exceptions, result values, or error codes?
- Can a worker or task be removed or reassigned?
- What exactly should the printed status look like?
- Is a completed task ever reopened?
What a strong answer covers
- Responsibilities:
Taskowns its state machine and allowed transitions,Workerowns its current task, and the manager checks both sides before changing either so a failed call leaves no half-update. - An explicit enum for task states and one place that defines legal transitions.
- Specific error types or messages for each invalid operation.
- A stable, sorted status built from immutable snapshots.
- Tests that walk one assign-complete journey and each rejected operation, checking state is unchanged after the error.
Follow-ups
- Let the manager pick a free worker automatically.
- Workers that can hold up to
ntasks. - Calls arriving from several threads at once.
Where it's asked
What it tests
object-oriented design · state machines
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
