Code Room
CodingMediumcod-g1066
Subject Machine learningLevel Mid–Senior~25 minCommon in ML systems interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.