Mini-Batch Data Loader
Updated
Problem statement
A training loop reads its dataset in small groups called mini-batches. Write the data loader. It makes epochs passes over data and returns every batch it produces, in order.
Let n = data.length. In each epoch:
- Start from the original order of positions
0, 1, ..., n - 1. - If
shuffleis true, shuffle the positions as described below. If it is false, keep them in order. - Cut the positions, in order, into batches of
batchSize. The last batch may be shorter. WithdropLastset to true, a last batch shorter thanbatchSizeis left out. - Each batch lists the values of
dataat its positions, in order.
Random numbers. The loader has one number generator. It starts with state = seed. Each time a number is needed, set state = (state * 1103515245 + 12345) mod 2^31 and use the new state. The generator is never reset, so it keeps going from one epoch to the next, and each epoch gets its own shuffle.
Shuffle. For i from n - 1 down to 1: take the next number x, let j = x mod (i + 1), and swap the positions at indexes i and j. When shuffle is false, no numbers are drawn.
state * 1103515245 can be close to 2.4 * 10^18, so use 64-bit integers for it. If no batch is produced, return an empty list.
Function
loadBatches(data: int[], batchSize: int, shuffle: boolean, dropLast: boolean, epochs: int, seed: int) → int[][]Examples
Example 1
data = [10,20,30,40,50]batchSize = 2shuffle = falsedropLast = falseepochs = 1seed = 0return = [[10,20],[30,40],[50]]No shuffling, so the items stay in their order. Cutting [10, 20, 30, 40, 50] into groups of 2 gives [10, 20], [30, 40] and a last group [50]. dropLast is false, so the short last batch is kept.
Example 2
data = [10,20,30,40,50]batchSize = 2shuffle = truedropLast = trueepochs = 2seed = 7return = [[10,30],[40,50],[30,40],[50,20]]Epoch 1 starts from positions [0, 1, 2, 3, 4]. Starting from the seed 7, the generator gives 1282168116, 642666333, 712265938 and 1486001571. Taking them modulo 5, 4, 3 and 2 gives j = 1, 1, 1, 1, so position 4 swaps with 1, then 3 with 1, then 2 with 1, then 1 with itself. The order becomes [0, 2, 3, 4, 1], which is the items [10, 30, 40, 50, 20]. The batches are [10, 30] and [40, 50], and the short batch [20] is dropped.
Epoch 2 starts again from [0, 1, 2, 3, 4], but the generator goes on: 2131988640, 220562521, 2099423262 and 2083449087 give j = 0, 1, 0, 1. The order becomes [2, 3, 4, 1, 0], the items [30, 40, 50, 20, 10], and [10] is dropped.
Example 3
data = [5,6,7]batchSize = 2shuffle = truedropLast = falseepochs = 1seed = 2return = [[7,5],[6]]The first number is (2 * 1103515245 + 12345) mod 2^31 = 59559187, and 59559187 mod 3 = 1, so position 2 swaps with position 1: [0, 2, 1]. The next number is 1495354192, and 1495354192 mod 2 = 0, so position 1 swaps with position 0: [2, 0, 1].
The items in that order are [7, 5, 6]. Batches of 2 give [7, 5] and the short batch [6], which is kept.
Constraints
1 <= data.length <= 1000-10^6 <= data[i] <= 10^61 <= batchSize <= 10001 <= epochs <= 10andepochs * data.length <= 30000 <= seed <= 2^31 - 1shuffleanddropLastare eachtrueorfalse.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Latency Bucket CounterDatadog · OA · PHONE SCREENDatadog interview questions
- Shuffle a Deck From Random ChoicesGoogle · PHONE SCREENGoogle interview questions
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Deterministic K-Means AssignmentsLyft · PHONE SCREENLyft interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions