Resumable Batched Data Loader
Problem statement
For this exercise, implement a resumable data loader over the fixed ordered array records. The loader starts before the first record, returns batches of at most batchSize records, and can save and restore its progress. It does not shuffle, restart epochs, or modify records.
Process operations in order. Every operation is a two-integer row:
[0,0]: read the next batch. Return up tobatchSizeconsecutive unread records and advance past them. IfdropLastis true and fewer thanbatchSizerecords remain, discard that tail, become exhausted, and return an empty row. An exhausted loader always returns an empty row.[1,slot]: save a snapshot of the current next-unread position inslot. Overwrite any previous snapshot in that slot and return an empty row.[2,slot]: restore the saved position in that slot and return an empty row. That slot is guaranteed to have been saved earlier.
Saved snapshots do not change when the active loader advances or restores another slot. Saving at the beginning or after exhaustion is valid. Return one int[] row per operation, including empty rows for save, restore, or exhausted reads. A restore may intentionally cause previously returned records to appear again.
Function
resumeBatches(records: int[], batchSize: int, dropLast: boolean, operations: int[][]) → int[][]Examples
Example 1
records = [10,20,30,40,50]batchSize = 2dropLast = falseoperations = [[0,0],[1,7],[0,0],[2,7],[0,0],[0,0],[0,0]]return = [[10,20],[],[30,40],[],[30,40],[50],[]]Slot7 captures the next unread record30. Restoring it repeats [30,40]; the final partial batch is kept and the next read is empty.
Example 2
records = [1,2,3,4,5]batchSize = 3dropLast = trueoperations = [[1,0],[0,0],[1,1],[0,0],[2,1],[0,0],[2,0],[0,0]]return = [[],[1,2,3],[],[],[],[],[],[1,2,3]]The two-record tail is dropped. Slot1 resumes at that tail and still yields no batch; slot0 restores the initial position.
Example 3
records = [-2,-2,7]batchSize = 1dropLast = falseoperations = [[1,4],[0,0],[1,4],[0,0],[2,4],[0,0],[0,0]]return = [[],[-2],[],[-2],[],[-2],[7]]Saving again overwrites slot4 with the position after the first record. Duplicate record values do not identify progress.
Constraints
0 <= records.length <= 200and1 <= batchSize <= 200.- Record values are integers in
-10^6..10^6; duplicates are allowed. 0 <= operations.length <= 200; each row has exactly two integers and follows one of the three operation forms.- Checkpoint slot IDs are in
0..100; every restore references a previously saved slot. - The total number of record values emitted across all returned rows is at most
4000. Save and restore rows contain no record values.