Does a Pod Fit on a Node
Problem Statement
Before scheduling, check whether a node has enough free CPU and memory for a pod's requests.
Background
A node has allocatable CPU and memory; some is already used. A pod fits if both its CPU request and memory request are <= the node's remaining CPU and memory respectively.
Your Task
def pod_fits(node, pod):
- node: dict with cpu, mem (allocatable) and used_cpu, used_mem.
- pod: dict with cpu, mem (requests).
- Return True if it fits, else False.
Input Format
- node (dict), pod (dict).
Output Format
- A boolean.
Sample
print(pod_fits({"cpu":4,"mem":8,"used_cpu":2,"used_mem":4}, {"cpu":1,"mem":2}))
Output:
True
Example:
print(pod_fits({"cpu":4,"mem":8,"used_cpu":2,"used_mem":4}, {"cpu":1,"mem":2}))True
- Calculate the node's available CPU by subtracting the used amount from the allocatable total: 4−2=2.
- Determine the node's available memory by subtracting the used memory from the allocatable memory: 8−4=4.
- Verify the pod's CPU request against the free CPU capacity: 1≤2 is true.
- Verify the pod's memory request against the free memory capacity: 2≤4 is true.
- Since both the CPU and memory requests fit within the remaining resources, the final output is True.
Constraints:
- Free cpu = cpu - used_cpu; free mem = mem - used_mem.
- Fits only if pod.cpu <= free cpu AND pod.mem <= free mem.
- Return a bool.
1. Background Knowledge
In Kubernetes, resource scheduling is the process of matching a pod's resource requests to a node's available capacity. Every node exposes allocatable resources, which represent the total CPU and memory available for pods after reserving a portion for system components (kubelet, container runtime, etc.). As pods are scheduled onto a node, the used resources increase, reducing the free capacity.
A pod specifies its resource requests in its manifest. These requests are the minimum amount of CPU and memory the pod needs to run. The Kubernetes scheduler will only place a pod on a node if the node has sufficient free resources to satisfy all of the pod's requests simultaneously. This is a multi-dimensional constraint check: both CPU and memory must have enough headroom.
The core concept here is resource accounting. For each resource dimension (CPU, memory), you compute the free capacity as:
free=allocatable−usedThe pod fits if and only if, for every resource dimension, the pod's request is less than or equal to the free capacity. This is a simple feasibility check with no optimization involved.
2. Algorithm Approach
This is a direct comparison problem. There is no search, sorting, or dynamic programming. The approach is:
- Compute the free CPU on the node: node["cpu"] - node["used_cpu"].
- Compute the free memory on the node: node["mem"] - node["used_mem"].
- Check whether the pod's CPU request is ≤ free CPU and the pod's memory request is ≤ free memory.
- Return the boolean result of that combined check.
This is an O(1) constant-time operation since it involves a fixed number of arithmetic and comparison operations regardless of input size.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.