PIXELBANKv9.1.0
Menu

Filter Nodes by Taints and Tolerations

Problem Statement

A pod can only schedule onto nodes whose NoSchedule taints it tolerates. Return the names of schedulable nodes.

Background

Each node has a list of taints (key, value, effect). Only effect == "NoSchedule" taints block scheduling. A pod carries tolerations (key, value). A node is schedulable if for every one of its NoSchedule taints, the pod has a matching toleration (same key and value). Taints with other effects are ignored here.

Your Task

def schedulable_nodes(nodes, tolerations):
  • nodes: list of {"name": str, "taints": [(key, value, effect), ...]}.
  • tolerations: list of (key, value) tuples.
  • Return the sorted list of schedulable node names.

Input Format

  • nodes (list of dicts), tolerations (list of tuples).

Output Format

  • A sorted list of strings.

Sample

nodes = [{"name":"n1","taints":[("gpu","true","NoSchedule")]}, {"name":"n2","taints":[]}]
print(schedulable_nodes(nodes, [("gpu","true")]))

Output:

['n1', 'n2']

Example:

Input:
nodes = [{"name":"n1","taints":[("gpu","true","NoSchedule")]}, {"name":"n2","taints":[]}]
print(schedulable_nodes(nodes, [("gpu","true")]))
Output:
['n1', 'n2']
Reasoning:
  • Convert the input tolerations into a set for efficient lookup: tol={("gpu","true")}\text{tol} = \{(\text{"gpu"}, \text{"true"})\}.
  • Evaluate node "n1": Identify its NoSchedule taints as [("gpu","true")][("gpu", "true")]. Since the pair ("gpu","true")("gpu", "true") exists in the toleration set, the node is schedulable.
  • Evaluate node "n2": It has an empty taint list, so there are no blocking taints. The condition is vacuously true, making the node schedulable.
  • Collect the names of all schedulable nodes: ["n1","n2"][\text{"n1"}, \text{"n2"}].
  • Sort the collected names alphabetically, which remains ["n1","n2"][\text{"n1"}, \text{"n2"}] in this case.
  • The final output is ['n1', 'n2']

Constraints:

  • Only NoSchedule taints block scheduling.
  • A node is OK if every NoSchedule taint's (key,value) is in tolerations.
  • Return node names sorted ascending.
🔒

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.