PIXELBANKv9.1.0
Menu

Spread Pods Across Zones for Anti-Affinity

Problem Statement

To satisfy topology spread constraints, place pods across zones so the difference between the most-loaded and least-loaded zone stays minimal. Report the max skew after greedily placing each pod in the currently-least-loaded zone.

Background

Given zones (each starting with some existing pod count) and a number of new pods to place, assign each new pod one at a time to the zone with the fewest pods (ties broken by zone index). After placing all, the skew is max(counts) - min(counts).

Your Task

def max_skew(initial, new_pods):
  • initial: list of ints, current pod count per zone.
  • new_pods: int, pods to place.
  • Return the final skew (int).

Input Format

  • initial (list of ints), new_pods (int).

Output Format

  • A single int.

Sample

print(max_skew([0, 0, 2], 3))

Output:

1

Example:

Input:
print(max_skew([0, 0, 2], 3))
Output:
1
Reasoning:
  • Start with the initial zone counts [0,0,2][0, 0, 2] and identify the zone with the fewest pods to place the first of the 3 new pods. Zones 0 and 1 are tied at 0, so the pod goes to Zone 0 (lowest index), updating counts to [1,0,2][1, 0, 2].
  • Place the second pod in the currently least-loaded zone. Zone 1 has 0 pods, which is less than Zone 0 (1) and Zone 2 (2), so the pod goes to Zone 1, updating counts to [1,1,2][1, 1, 2].
  • Place the third pod in the least-loaded zone. Zones 0 and 1 are tied at 1 pod, so the pod goes to Zone 0 (lowest index), updating counts to [2,1,2][2, 1, 2].
  • Calculate the final skew by finding the difference between the maximum and minimum pod counts in the final state [2,1,2][2, 1, 2]: skew=max⁡(2,1,2)−min⁡(2,1,2)=2−1=1\text{skew} = \max(2, 1, 2) - \min(2, 1, 2) = 2 - 1 = 1.
  • The final output is 1

Constraints:

  • Place each pod into the least-loaded zone (ties: smaller index).
  • Skew = max(counts) - min(counts) after all placements.
  • initial is non-empty.
🔒

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.
Spread Pods Across Zones for Anti-Affinity - Medium | PixelBank