← Questions

Assign tasks to servers by weighted load with expiry

0
StripeCodingLast asked Nov 2023

A coding interview question asked in Stripe's onsite coding (debug, integrate, build) round, last asked Nov 2023. It tests heap, simulation and load balancing.

Assign tasks to servers while tracking each server's active load. Each server has a maximum load. Tasks arrive in time order, each with a weight and a time to live (TTL). Before each task, release every earlier task whose time has run out, then place the new task on the least-loaded server that still has room for it.

Interface

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

  • assign(servers: list[str], tasks: list[str]) -> list[str]
    • Each server row is name,max_load; each task row is time,task_id,weight,ttl.
    • A task placed at time is active until time + ttl. Before placing a task at time t, expire every task with end <= t and remove its weight from its server.
    • A server can take the task if load + weight <= max_load. Pick the one with the smallest current load, ties broken by the smaller name.
    • Return task_id:server_name per task, or task_id:NONE if no server can take it. A rejected task uses no capacity.

Example

servers = ["a,5", "b,4"]
tasks   = ["0,t1,3,5", "1,t2,2,3", "4,t3,4,2", "5,t4,5,1"]
->  ["t1:a",      # both empty; a wins the tie on name
     "t2:b",      # b (load 0) is lighter than a (load 3)
     "t3:b",      # t2 ended at 4; a has 3 and cannot fit 4 more
     "t4:a"]      # t1 ended at 5, so a is empty again

servers = ["solo,3"]
tasks   = ["0,t1,3,5", "0,t2,1,2", "5,t3,3,1"]
->  ["t1:solo", "t2:NONE", "t3:solo"]

Constraints

  • Up to 1,000 servers and 10,000 tasks. Times never decrease; tasks at the same time are handled in input order.
  • Loads, weights and TTLs are positive integers up to 10^9; end times fit in 64 bits.

Clarifying questions

  • Does a task that ends exactly at time t free its capacity for a task arriving at t? (Yes.)
  • Is "least loaded" by absolute load or by fraction of capacity?
  • Should rejected tasks wait in a queue for capacity?
  • Can servers be added or removed during the run?

What a strong answer covers

  • A min-heap of (end_time, server, weight) so expiry pops only the tasks that are done; O(log n) each.
  • Choosing a server: a linear scan is O(S) per task and fine here; a sorted structure keyed by (load, name) gives O(log S) lookup but needs care because "has room" depends on each server's capacity.
  • Correct order of operations: expire, then choose, then add.
  • Tests: exact-boundary expiry, a task heavier than every server, ties on load and name.

Follow-ups

  • Make server selection faster than O(S) per task.
  • Rejected tasks queue and are retried when capacity frees up.
  • Servers can fail; reassign their active tasks.
  • Weight the choice by remaining capacity instead of load.

Where it's asked

  • Stripe · Onsite coding: debug, integrate, build · Last asked Nov 202360 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 · simulation · load balancing

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