← Questions

Load balancer with worker states, a priority queue and autoscaling

0
Scale AICodingLast asked Dec 2025

A coding interview question asked in Scale AI's coding round, last asked Dec 2025. It tests load balancing, state machines and heaps.

In a time-boxed round, write a small load balancer in Python that dispatches tasks to a pool of workers, and explain its design. Workers move through well-defined states, tasks wait in a priority queue, slow or failed work is retried, and the pool grows and shrinks with load. The code should run.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • Worker states IDLE, BUSY, DRAINING, FAILED, with transitions IDLE -> BUSY -> IDLE, BUSY -> FAILED, IDLE -> DRAINING -> removed, BUSY -> DRAINING (finishes its task, then is removed). Any other transition is an error.
  • submit(task_id: str, priority: int, deadline: float) -> None queues a task; higher priority first, then earlier submission.
  • dispatch(now: float) -> list[tuple[str, str]] assigns queued tasks to IDLE workers and returns (task_id, worker_id) pairs. Tasks past their deadline are dropped as expired.
  • complete(worker_id: str, task_id: str) -> None, fail(worker_id: str) -> None and tick(now: float) -> None (timeouts: a task running longer than task_timeout is requeued and its worker marked FAILED).
  • scale(now: float) -> None adds workers when the queue is long or utilization is high, and drains idle workers when load falls, within min_workers and max_workers.

Example

lb = LoadBalancer(min_workers=1, max_workers=3, task_timeout=10)   # starts with worker w1
lb.submit("a", priority=1, deadline=100)
lb.submit("b", priority=5, deadline=100)
lb.dispatch(now=0)          -> [("b", "w1")]        # higher priority first; one idle worker
lb.scale(now=0)             -> adds w2 (queue not empty, no idle workers)
lb.dispatch(now=1)          -> [("a", "w2")]
lb.complete("w2", "a")      -> w2 IDLE            # at time 5
lb.tick(now=12)             -> "b" has run 12 > 10: requeued, w1 FAILED
lb.dispatch(now=12)         -> [("b", "w2")]

Constraints

  • At-least-once processing: a task is never lost; duplicates are kept to a minimum.
  • A worker being drained never receives a new task.

Clarifying questions

  • Are workers threads, processes or remote machines?
  • What happens to a task that keeps failing: retry forever, or a limit and a dead-letter list?
  • Which signals drive scaling: queue length, wait time, utilization?
  • How are ties in priority ordered?

What a strong answer covers

  • Classes Worker, Task, LoadBalancer, with a transition table that rejects invalid state changes.
  • A heap keyed by (-priority, sequence), lazy removal of expired tasks, and a map of running tasks to workers and start times.
  • Timeouts and failure detection (heartbeats for remote workers), requeueing with a retry count, and idempotent completion so a late result after a retry is ignored.
  • Scaling with hysteresis and a cooldown so the pool doesn't flap; draining that never loses or duplicates a task.
  • Concurrency: one lock around shared state, or a single asyncio event loop; why threads suit I/O-bound tasks and processes CPU-bound ones in Python.
  • Tests with a fake clock for timeouts, expiry and scaling.

Follow-ups

  • Make it thread-safe with real worker threads.
  • A worker that was marked failed comes back and reports completion. What happens?
  • Add per-tenant fairness so one tenant's flood of high-priority tasks doesn't starve others.

Where it's asked

  • Scale AI · Coding · Last asked Dec 2025Live coding, one or two rounds, Python

What it tests

load balancing · state machines · heaps · python

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding