K-Fold Size Allocation

Beginner
~10 min
code completion

K-Fold Size Allocation

K-fold cross-validation partitions n samples into k non-overlapping folds. Each fold serves as the validation set once while the other folds form the training set.

When n is not evenly divisible by k, the sizes can not all be equal. The standard convention:

  • The first n % k folds get size n // k + 1
  • The remaining folds get size n // k
  • This ensures the sizes sum to exactly n.

    Example: n = 10, k = 3

  • base = 10 // 3 = 3, remainder = 10 % 3 = 1
  • Fold 0 gets 4, folds 1 and 2 get 3
  • Result: [4, 3, 3] — verify: 4 + 3 + 3 = 10 ✓
  • Your task:

    Implement kfold_sizes(n, k) that returns a Python list of k integers summing to n.

    Example Tests

    10 samples, 3 folds: remainder 1 goes to first fold

    Input: {"k":3,"n":10}

    Expected: [4,3,3]

    Evenly divisible: all folds have equal size

    Input: {"k":4,"n":12}

    Expected: [3,3,3,3]

    7 samples, 3 folds: remainder 1 to first fold

    Input: {"k":3,"n":7}

    Expected: [3,2,2]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.