The Chunked Reader
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L3
Problem
Given a list of lines and a positive chunk_size, return a list of successive chunks, each a list of at most chunk_size lines, in order. The last chunk may be smaller than chunk_size if the elements run out. For lines=['a','b','c','d','e'] and chunk_size=2, return [['a','b'],['c','d'],['e']]. An empty input returns an empty list. Build the chunks lazily with a generator for memory efficiency, then materialize them into the returned list of lists.
Summary
Too big for memory. Read in pieces.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.