Code RoomK-fold cross-validation splits
MediumPrep Room Coding #230

K-fold cross-validation splits

CodingML systemsMid–Senior~25 min

Generate k-fold cross-validation index splits for a dataset of n samples (indices 0..n-1, in order, no shuffling). Split the indices into k contiguous folds as evenly as possible: if n is not divisible by k, the first (n mod k) folds get one extra element. For each fold i, that fold is the validation set and the remaining indices (in ascending order) form the training set. Return a list of k pairs [train_indices, val_indices]. Assume 1 <= k <= n.

Implement
kfold_indices(n: int, k: int) → list[list[list[int]]]
Examples
in[6,3]out[[[2,3,4,5],[0,1]],[[0,1,4,5],[2,3]],[[0,1,2,3],[4,5]]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[6,3][[[2,3,4,5],[0,1]],[[0,1,4,5],[2,3]],[[0,1,2,3],[4,5]]]not run yetsample