Walk-Forward Cross-Validation Splits

~15 mincode completion

Implement walkforward_splits(n, window_size, step) that returns a list of [train_end, test_end] pairs.

Examples

10 timesteps, window=4, step=2 produces 3 splits

Input
walkforward_splits(10, 4, 2)
Output
[[4, 6], [6, 8], [8, 10]]

Only one complete split fits before running out of data

Input
walkforward_splits(8, 5, 3)
Output
[[5, 8]]

12 timesteps split into 2 equal non-overlapping windows

Input
walkforward_splits(12, 4, 4)
Output
[[4, 8], [8, 12]]

Hints

Hint 1

Build the result up as you go, then return it.

Hint 2

Watch for this: included incomplete splits where test end exceeds n.

Requirements

  • n: Total number of timesteps

  • window_size: Initial training window size

  • step: Number of timesteps per test window (and window advance)

  • Return List of [train_end, test_end] pairs where test_end <= n.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 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 walkforward_splits(n: int, window_size: int, step: int) -> list:
    """
    Generate walk-forward cross-validation split boundaries.

    Args:
        n:           Total number of timesteps
        window_size: Initial training window size
        step:        Number of timesteps per test window (and window advance)

    Returns:
        List of [train_end, test_end] pairs where test_end <= 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.