← Questions

Least-loaded task assignment with worker specialties

0
StripeCodingLast asked Jan 2026

A coding interview question asked in Stripe's onsite coding (debug, integrate, build) round, last asked Jan 2026. It tests heap, greedy and simulation.

Tasks arrive in a fixed order and must be handed out to a team of workers so that the work stays balanced.

Each task has a unique integer id, a positive integer duration, and (in Part 2) an optional required specialty. Each worker has a unique name and (in Part 2) a set of specialties.

Process the tasks one at a time, in the given order. A worker's load is the sum of the durations already assigned to it; every worker starts at 0. Give each task to the eligible worker with the lowest load. If several eligible workers share the lowest load, pick the one whose name is lexicographically smallest (plain string comparison).

@dataclass(frozen=True)
class Task:
    id: int
    duration: int
    specialty: str | None = None

def distribute(
    workers: dict[str, set[str]], tasks: list[Task]
) -> tuple[dict[str, list[int]], list[int]]: ...

Return a map from each worker name to the ids of its tasks, in assignment order (workers with no tasks map to []), and the list of task ids that could not be assigned.

Part 1. No task has a specialty, so every worker is eligible for every task.

Part 2. A task with a specialty may only go to workers whose set contains it. A task with specialty = None may go to anyone. If no worker has the required specialty, the task is not assigned, no load changes, and its id goes in the unassigned list.

Constraints

  • 1 <= len(workers) <= 10^5, 0 <= len(tasks) <= 10^5
  • 1 <= duration <= 10^6
  • Each worker has at most 10 specialties.

Example 1 (Part 1)

workers = {"bo": {}, "al": {}, "cy": {}}
tasks   = [(10, 4), (11, 2), (12, 3), (13, 1)]   # (id, duration)

10 -> al   loads al=4 bo=0 cy=0   (all tied at 0; "al" is smallest)
11 -> bo   loads al=4 bo=2 cy=0
12 -> cy   loads al=4 bo=2 cy=3
13 -> bo   loads al=4 bo=3 cy=3

result: {"al": [10], "bo": [11, 13], "cy": [12]}, unassigned []

Example 2 (Part 2)

workers = {"alice": {"billing"}, "bob": {"billing", "fraud"}, "cara": {}}
tasks   = [(1, 5, None), (2, 3, "fraud"), (3, 4, "billing"),
           (4, 2, None), (5, 1, "risk"), (6, 6, None)]

1 -> alice  (everyone at 0)
2 -> bob    (only bob has fraud)
3 -> bob    (billing: alice=5, bob=3)
4 -> cara   (alice=5, bob=7, cara=0)
5 -> none   (nobody has risk)
6 -> cara   (alice=5, bob=7, cara=2)

result: {"alice": [1], "bob": [2, 3], "cara": [4, 6]}, unassigned [5]

Aim for better than scanning every worker for every task.

Clarifying questions

  • Is a worker with specialties also eligible for tasks with no specialty? (Yes.)
  • Is the tie-break plain string comparison, so uppercase names sort before lowercase ones?
  • Can task ids repeat? Must the output keep each worker's tasks in assignment order?
  • Should a worker who gets no task still appear, mapped to []?

What a strong answer covers

  • Part 1: one min-heap of (load, name). Pop the top, assign the task, push the worker back with the new load. O(T log W).
  • Part 2: one heap per specialty plus one over all workers, so a worker sits in at most 11 heaps. When a worker's load changes, push fresh entries and skip stale ones when popped (compare with the worker's current load). O(T · S · log W) with S <= 10.
  • A task whose specialty nobody has goes to the unassigned list and changes no load.
  • Handles an empty task list, ties at load 0, and many workers sharing one specialty.
  • Tests both examples, including task 5 that nobody can take.

Follow-ups

  • Workers have a capacity; a task goes unassigned when every eligible worker is full.
  • Tasks finish and loads go down; workers join and leave between tasks.
  • A task needs several specialties at once.
  • All tasks are known up front and the goal is the smallest maximum load: why that is NP-hard, and a good greedy.

Where it's asked

  • Stripe · Onsite coding: debug, integrate, build · Last asked Jan 202660 min · Project editor with starter code and tests: fix bugs in an unfamiliar codebase, wire an external API spec into a service, or complete a skeleton in parts · Varies by session: some onsites have one AI-assisted session and an integration session without AI

What it tests

heap · greedy · simulation

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