# Batch Records

> Too many at once. Break them into groups.

Canonical URL: <https://datadriven.io/problems/batch_records>

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a list and positive integer n, return a list of sublists where each sublist has at most n elements. Items appear in original order. The final sublist may be shorter than n.

## Worked solution and explanation

### Why this problem exists in real interviews

Chunking a list into fixed-size batches is a fundamental data engineering primitive. It tests **index arithmetic**, **slice semantics**, and whether you understand that the final batch may be smaller than the requested size.

---

### Break down the requirements

#### Step 1: Iterate in steps of n

Use a range with step size n to generate start indices for each batch. This naturally handles the final partial batch.

#### Step 2: Slice the list for each batch

Python slicing `lst[i:i+n]` gracefully handles the case where fewer than n elements remain.

#### Step 3: Collect and return all batches

Accumulate each slice into a result list and return it.

---

### The solution

**Fixed-size chunking with range and slicing**

```python
def chunk_list(lst, n):
    batches = []
    for i in range(0, len(lst), n):
        batch = lst[i:i + n]
        batches.append(batch)
    return batches
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the length of the list. Each element is copied exactly once.
> 
> **Space:** O(n) for the output list of batches.

> **Interviewers Watch For**
>
> Using `range(0, len(lst), n)` shows you understand step arguments. Candidates who write a while loop with manual index tracking are correct but signal less Python fluency.

> **Common Pitfall**
>
> Off-by-one errors when computing the number of batches manually. Using `range` with a step avoids this entirely since Python handles the boundary.

---

## Common follow-up questions

- What if the input is a generator instead of a list? _(Tests whether you can batch from an iterator using itertools.islice without materializing the full sequence.)_
- How would you batch records while keeping related items together? _(Tests awareness of partitioning vs. simple chunking.)_
- What if n is zero or negative? _(Tests input validation and edge case handling.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/batch_records)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.