How would you process a large document stream without loading it all into memory?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
Iterators can produce records incrementally. Batching bounds the working set, but batch size must also respect payload limits, provider quotas and output handling.
An ingestion job has many document records but can only send a small bounded batch to an embedding service at a time.
from itertools import islice
def batches(iterable, size):
if isinstance(size, bool) or not isinstance(size, int) or size < 1:
raise ValueError("size must be a positive integer")
items = iter(iterable)
while True:
batch = list(islice(items, size))
if not batch:
return
yield batch
print(list(batches(range(5), 2)))
print(list(batches([], 2)))
try:
list(batches(range(3), 0))
except ValueError:
print("Rejected invalid size")Show expected output
[[0, 1], [2, 3], [4]]
[]
Rejected invalid sizeCopy and paste to run in a separate tab. This example illustrates one concept; it is not a complete application or an automatically graded task.
A practical way to learn it
Run the batching helper for empty input, five records and an invalid size. Explain which memory remains bounded and which output collection grows.
Open Python playgroundThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would consume the source incrementally and form bounded batches, validating the batch size and processing the final partial batch. I would avoid converting the entire input iterator to a list before batching. I would distinguish the count of records from their byte or token size and apply whichever limit is reached first. Results need stable IDs and incremental persistence so a failure does not require restarting all completed work. I would test empty input, an exact multiple, a partial batch and an oversized record, and measure actual memory use on representative inputs.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Building list(all_documents) and only then splitting it into chunks.
Now go one level deeper
- How would you resume after batch seven fails?
- How would you handle one record larger than the provider limit?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.