Skip to content
Coding · Medium · Capacity · Target O(p log p + p·n) · Senior

Bin-pack pods onto nodes

Place pods with CPU and memory requests onto nodes by first-fit decreasing, with a fully specified order, and report the pods that fit nowhere.

Statement

Pods and nodes are (name, cpu_millicores, memory_mib) tuples: for a pod the numbers are what it requests, for a node what it can hold. Write schedule(pods, nodes) that places pods by first-fit decreasing and returns (placement, unschedulable).

Order: try pods by CPU request, largest first. Break ties by memory request, largest first, and any remaining ties by name, A to Z.

Fit: a pod goes on the first node, in the order nodes were given, whose remaining CPU and remaining memory are both at least what the pod requests. An exact fit counts. Placing a pod reduces what that node has left. A pod that fits on no node is unschedulable and uses nothing.

placement is a dict of pod name to node name. unschedulable is a list of pod names in the order they were tried. Leave the input lists as they were.

Examples

The pods are tried in the order batch, db, web, cache. db needs 2000m of CPU: node-a has 1000m left after batch, and node-b has the CPU but only 4096 MiB of memory.

nodes = [("node-a", 4000, 8192), ("node-b", 2000, 4096)]
pods  = [("web", 1500, 2048), ("db", 2000, 6144),
         ("cache", 800, 2048), ("batch", 3000, 1024)]

→ ({"batch": "node-a", "web": "node-b", "cache": "node-a"}, ["db"])

Constraints

Up to 2,000 pods and 500 nodes. Names are unique among pods and among nodes. Requests and capacities are non-negative integers. A pod that requests nothing fits on any node; with no nodes, every pod is unschedulable.

Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app