Allocate servers from a pool of mixed node types
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Sep 2025. It tests data structure design and scheduling.
A service manages a pool of about 100,000 server nodes. Each node can serve one or more instance types. Clients ask for a number of servers that support the instance types they need, and later return them. Design the allocator: its API, the data structures and indexes that make matching fast, how availability is tracked, how to keep the pool from fragmenting, and how it behaves under concurrency and failures.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
add_node(node_id: str, types: set[str]) -> None— register a node and the instance types it can serve. -
get_servers(count: int, types: list[str]) -> list[str] | None— reservecountfree nodes that each support every listed type, or returnNoneand reserve nothing if there aren't enough. -
return_servers(node_ids: list[str]) -> None— release nodes; returning a node that isn't allocated is ignored (or reported).
Example
add_node("n1", {"cpu"})
add_node("n2", {"cpu", "gpu"})
add_node("n3", {"cpu", "gpu"})
get_servers(1, ["gpu"]) -> ["n2"]
get_servers(2, ["gpu"]) -> None # only n3 still has a free gpu
get_servers(1, ["cpu"]) -> ["n1"] # prefer the node that can only do cpu, keep n3 for gpu work
return_servers(["n2"])
get_servers(2, ["cpu", "gpu"]) -> ["n2", "n3"]
Constraints
- About 10^5 nodes and a modest number of distinct instance types.
- Many concurrent clients; requests should be answered in milliseconds.
Clarifying questions
- Must each node support all requested types, or must the set of nodes cover them together?
- All-or-nothing, or partial allocations?
- Do nodes have capacity, so several instances can share one, or is a node allocated whole?
- Should requests wait in a queue when the pool is short? Is there priority?
- What happens to allocated nodes when a client crashes?
What a strong answer covers
- An index from each type (or each exact type set) to its free nodes, as sets for O(1) add and remove; a request intersects or picks the smallest candidate set.
- Grouping nodes by their exact capability set, so a request scans a few groups instead of many nodes.
- Fragmentation: prefer the least capable node that satisfies the request, so scarce nodes (GPUs) stay free for requests that need them.
- All-or-nothing reservation with a rollback when the count can't be met.
- Concurrency: one lock per capability group or optimistic reservation with compare-and-set; avoid deadlock by locking groups in a fixed order.
- Starvation: large requests that never fit; a fair queue or reservations that hold capacity for them.
- Failures: leases with a time-to-live so nodes from crashed clients come back; node health checks that remove failed nodes from the free sets; persisting allocations so a restarted allocator recovers.
Follow-ups
- Nodes have CPU and memory capacities and requests ask for amounts (bin packing).
- Spread a request's nodes across racks or zones.
- Scale the allocator itself to several replicas.
Where it's asked
- Google · First round: coding · Last asked Sep 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
data structure design · scheduling
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
