← 返回 anthropic 的题目列表Design a Data Batcher (Code & Design)
类型:online_judge
Question 2: Code & Design — Design and implement a Data Batcher
Design a data batcher to organize samples into training batches and iterate efficiently within a training loop.
Requirements
Input:
A data source dataset (assume an iterable, or a container with __len__ and __getitem__).
batch_size.
Output:
An iterable batcher/iterator that yields one batch at a time (a list of samples or a dict of tensors).
Batching behavior (common follow-ups):
Support shuffle (train) vs no shuffle (eval).
Handle the last incomplete batch (drop/keep/pad).
Reproducibility via seed.
Optional prefetch or lightweight concurrent loading (design discussion is fine).
Variable-length samples and a collate function (padding + mask).
Tasks
Propose an API (class/function signature).
Describe core data structures and time/space complexity.
Provide a core implementation (Python pseudocode or runnable code).
Explain how you would test it (at least 3 test ideas).
Constraints
Dataset size: 1e5–1e8 samples
Limited memory; cannot necessarily load all indices/contents at once
Throughput should be high enough to avoid becoming a training bottleneck