Automation

Rollout batches

Split an ordered host list into rollout waves without exceeding the specified batch size.

In production

Canary batches limit blast radius. Off-by-one slices either skip hosts or dump the remainder into one unsafe batch.

What it must return

Split hosts into contiguous batches of batch_size, with a final shorter batch if needed.

  • batch_size is a positive integer. Preserve order and duplicates. Every batch except the last must contain batch_size hosts. Empty input returns an empty list.
  • Do not mutate input values. Use the Python standard library only.

Edge cases

  • Advance the start index by batch_size each time.
  • A slice handles the final partial batch.
  • Empty hosts yield no batches.

Signature

rollout_batches(hosts, batch_size)

Hints

  1. Second hint

  2. A partial sketch

The reference solution opens after the last hint.

solution.py · Python

Loading editor…

Partial final batch

hosts

[
  "a",
  "b",
  "c",
  "d",
  "e"
]

batch_size

2

Expected

[
  [
    "a",
    "b"
  ],
  [
    "c",
    "d"
  ],
  [
    "e"
  ]
]

Each run uses a fresh Python runtime in this browser, with a 3-second limit and a Stop button. The first run downloads the runtime. 0 of 8 in Automation passed in Python on this device.

Next: Hosts low on disk