Rolling-update simulation

A free medium coding practice problem from devops. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Medium
Type
Function
Topic
Devops
Languages
JavaScript, TypeScript or Python

Problem

Simulate a simplified rolling update. The deployment wants replicas pods; it starts with old = replicas pods of the old version and new = 0. All existing pods are ready at the start of each step. Each step does, in order:
1. kill = max(0, min(old, old + new − (replicas − maxUnavailable))) old pods are removed;
2. create = max(0, min(replicas − new, replicas + maxSurge − old − new)) new pods are created (using old AFTER the kill); they are ready by the next step.

Steps repeat until old = 0 and new = replicas. Return the list of [old, new] after each step. 1 ≤ replicas ≤ 50, 0 ≤ maxSurge, maxUnavailable ≤ replicas, and they are not both 0.

Function signature

// JavaScript
function rolloutSteps(replicas, maxSurge, maxUnavailable)

// TypeScript
function rolloutSteps(replicas: any, maxSurge: any, maxUnavailable: any): any

// Python
def rollout_steps(replicas, maxSurge, maxUnavailable):

Examples

  1. rolloutSteps(3, 1, 0) → [[3,1],[2,2],[1,3],[0,3]]
  2. rolloutSteps(3, 0, 1) → [[2,1],[1,2],[0,3]]
  3. rolloutSteps(4, 2, 2) → [[2,4],[0,4]]

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps