Batch a stream with a size limit
Python data engineering interview problem. Difficulty: intermediate. Pattern: Streaming. About 15 minutes. Part of the Pro drill bank.
An event stream should be flushed to disk in batches of at most N items so a crash only loses one batch.
An export job sends rows to an API that accepts only a few items per request. It needs to cut the list into batches. Split the items into lists of size items in order. The last batch can be smaller. If size is less than 1, return an empty list. Write batched(items, size). [1, 2, 3, 4, 5] with size 2 gives [[1, 2], [3, 4], [5]]. An empty list gives []. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Every batch has size items, except maybe the last.
- Keep the order of the items.
- Return an empty list when size is less than 1.
- Return [] for no items.
Constraints
- items is a list (or any iterable).
- size is a whole number.
Examples
Input: batched([1, 2, 3, 4, 5], 2) Output: [[1, 2], [3, 4], [5]] This input follows the stated rules and produces this output.
Topics: lakebench, python, generators.
More Python interview questions · All interview problems · Learn data engineering
Batch a stream with a size limit
Interview-style drill: Write a generator that yields fixed-size batches from an arbitrarily long iterable.
Split a sequence into batches of a given size.