PIXELBANKv9.1.0
Menu

Minimum Rollout Steps Under Surge and Unavailability

Problem Statement

Simulate a rolling update and count how many batch steps it takes to move from all-old to all-new pods, respecting maxSurge and maxUnavailable.

Background

Start with replicas old pods, target replicas new pods. Total pods may range within [replicas - maxUnavailable, replicas + maxSurge]. Each step, the controller: (1) creates as many new pods as surge allows (up to replicas + maxSurge total), then (2) deletes old pods as long as available (non-terminating) pods stay >= replicas - maxUnavailable. Count steps until all pods are new. Model each step as: can_add = (replicas + maxSurge) - (old + new); add that many new; then can_remove = (new + old) - (replicas - maxUnavailable) old pods removed (bounded by old remaining and by new pods created so far able to serve). Return the number of steps. Assume maxSurge + maxUnavailable >= 1 so progress is always possible.

Your Task

def rollout_steps(replicas, max_surge, max_unavailable):

Return the number of steps (int) to fully roll out.

Input Format

  • replicas (int), max_surge (int), max_unavailable (int).

Output Format

  • A single int.

Sample

print(rollout_steps(4, 1, 1))

Output:

3

Example:

Input:
print(rollout_steps(4, 1, 1))
Output:
3
Reasoning:
  • Initialization: Start with old = 4 (all pods are old) and new = 0. The target is new = 4. The minimum allowed total pods is 4βˆ’1=34 - 1 = 3, and the maximum allowed is 4+1=54 + 1 = 5.

  • Step 1:

    • Add: We can add up to 5βˆ’(4+0)=15 - (4 + 0) = 1 new pod. So, new becomes 0+1=10 + 1 = 1.
    • Remove: The current total pods is 4+1=54 + 1 = 5. We can remove up to 5βˆ’3=25 - 3 = 2 old pods, but we are limited by the number of new pods created so far (1) to ensure service availability. Thus, we remove 1 old pod. old becomes 4βˆ’1=34 - 1 = 3.
    • State: old = 3, new = 1.
  • Step 2:

    • Add: We can add up to 5βˆ’(3+1)=15 - (3 + 1) = 1 new pod. So, new becomes 1+1=21 + 1 = 2.
    • Remove: The current total pods is 3+2=53 + 2 = 5. We can remove up to 5βˆ’3=25 - 3 = 2 old pods. We are limited by the number of new pods (2), so we remove 2 old pods. old becomes 3βˆ’2=13 - 2 = 1.
    • State: old = 1, new = 2.
  • Step 3:

    • Add: We can add up to 5βˆ’(1+2)=25 - (1 + 2) = 2 new pods. We need 4βˆ’2=24 - 2 = 2 more to reach the target. So, we add 2. new becomes 2+2=42 + 2 = 4.
    • Remove: The current total pods is 1+4=51 + 4 = 5. We can remove up to 5βˆ’3=25 - 3 = 2 old pods. We only have 1 old pod left, so we remove 1. old becomes 1βˆ’1=01 - 1 = 0.
    • State: old = 0, new = 4.
  • The loop terminates because new has reached replicas (4). The total number of steps taken is 3.

  • The final output is 3

Constraints:

  • Pods count stays within [replicas - maxUnavailable, replicas + maxSurge].
  • Each step: add new up to surge cap, then remove old down to the availability floor.
  • Return the step count; maxSurge + maxUnavailable >= 1.
πŸ”’

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.