Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

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.

Split a sequence into batches of a given size.

Requirements

  • Every item appears in exactly one batch.
  • The last batch may be shorter.

Constraints

  • items: the stream as a list.
  • size: max batch length.
  • size is at least 1.

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

intermediate

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.