K-Fold Size Allocation

~10 mincode completion

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

Examples

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

Input
kfold_sizes(10, 3)
Output
[4, 3, 3]

Evenly divisible: all folds have equal size

Input
kfold_sizes(12, 4)
Output
[3, 3, 3, 3]

7 samples, 3 folds: remainder 1 to first fold

Input
kfold_sizes(7, 3)
Output
[3, 2, 2]

Hints

Hint 1

The remainder operator gives what is left after the division.

Hint 2

Watch for this: distributed remainder to last folds not first.

Requirements

  • n: Total number of samples

  • k: Number of folds

  • Return List of k integers summing to n.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

8 employers weight this skill

3 quant funds, 2 big tech firms, 1 health and bio company, 1 AI product company, 1 defense company. Top match scores 75.

Python
def kfold_sizes(n: int, k: int) -> list:
    """
    Compute the size of each fold for k-fold cross-validation.
    The first (n % k) folds get size (n // k + 1); the rest get (n // k).

    Args:
        n: Total number of samples
        k: Number of folds

    Returns:
        List of k integers summing to n.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.