PIXELBANKv9.1.0
Menu

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:

Input:
print(pod_fits({"cpu":4,"mem":8,"used_cpu":2,"used_mem":4}, {"cpu":1,"mem":2}))
Output:
True
Reasoning:
  • Calculate the node's available CPU by subtracting the used amount from the allocatable total: 4−2=24 - 2 = 2.
  • Determine the node's available memory by subtracting the used memory from the allocatable memory: 8−4=48 - 4 = 4.
  • Verify the pod's CPU request against the free CPU capacity: 1≤21 \le 2 is true.
  • Verify the pod's memory request against the free memory capacity: 2≤42 \le 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.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.