Three-Way Train/Validation/Test Hold-Out Split

Quick Overview

Write a function that splits sample indices into disjoint train, validation and test sets from three fractions, with optional seeded shuffling. It tests careful input validation, floating point care when turning fractions into counts, and why a hold-out split keeps model evaluation honest.

Three-Way Train/Validation/Test Hold-Out Split

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `train_val_test_split_indices(n_samples, train_frac, val_frac, test_frac, shuffle=False, random_state=None)` that partitions `range(n_samples)` into disjoint train, validation and test index sets. Return a tuple `(train_idx, val_idx, test_idx)` of NumPy integer arrays, each sorted ascending. Requirements: - Sizes: `n_train = floor(train_frac * n + 1e-9)`, `n_val = floor(val_frac * n + 1e-9)`, and test gets the remainder. - The order is `np.arange(n)` if not shuffled, else `np.random.default_rng(random_state).permutation(n)`. The first `n_train` positions go to train, the next `n_val` to validation, and the rest to test. - A non-integer `n_samples` (including `bool`) or `n_samples < 3` raises `ValueError`. - Any fraction that is non-finite or not strictly inside (0, 1), or fractions that do not sum to 1 within a tolerance of `1e-8`, raise `ValueError`. - Any empty partition raises `ValueError`. Example: ```python train_val_test_split_indices(11, 0.5, 0.3, 0.2) # -> ([0 1 2 3 4], [5 6 7], [8 9 10]) train_val_test_split_indices(5, 0.8, 0.1, 0.1) # -> ValueError (n_val = 0) ``` ```hint Two traps in the checks In Python, `isinstance(True, int)` is `True`, and a NaN fraction makes every comparison return `False`. Decide the order of your validation checks with both facts in mind. ``` ### What a Strong Answer Covers - Input validation in a safe order: an integer type with `bool` excluded, the minimum size, finite fractions inside the open interval, and the sum tolerance. - Partition sizes from the floor-plus-epsilon rule, with test taking the remainder and a check that no part is empty. - A single ordering (identity or seeded permutation) cut into consecutive blocks, so the parts are disjoint and together cover every index. - Sorted integer outputs and the time complexity. ### Follow-up Questions - How would you change the function so each part keeps the class proportions of a label array? - When would a random three-way split give a misleading test score? - Why keep a separate test set when you already choose hyperparameters on a validation set? - When is k-fold cross-validation a better choice than one hold-out validation set?

Overview: Write a function that splits sample indices into disjoint train, validation and test sets from three fractions, with optional seeded shuffling. It tests careful input validation, floating point care when turning fractions into counts, and why a hold-out split keeps model evaluation honest.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement train_val_test_split_indices(n_samples, train_frac, val_frac, test_frac, shuffle=False, random_state=None) that partitions range(n_samples) into disjoint train, validation and test index sets. Return a tuple (train_idx, val_idx, test_idx) of NumPy integer arrays, each sorted ascending.

Requirements:

  • Sizes: n_train = floor(train_frac * n + 1e-9) , n_val = floor(val_frac * n + 1e-9) , and test gets the remainder.
  • The order is np.arange(n) if not shuffled, else np.random.default_rng(random_state).permutation(n) . The first n_train positions go to train, the next n_val to validation, and the rest to test.
  • A non-integer n_samples (including bool ) or n_samples < 3 raises ValueError .
  • Any fraction that is non-finite or not strictly inside (0, 1), or fractions that do not sum to 1 within a tolerance of 1e-8 , raise ValueError .
  • Any empty partition raises ValueError .

Example:

train_val_test_split_indices(11, 0.5, 0.3, 0.2)
# -> ([0 1 2 3 4], [5 6 7], [8 9 10])

train_val_test_split_indices(5, 0.8, 0.1, 0.1)
# -> ValueError (n_val = 0)

What a Strong Answer Covers Guidance

  • Input validation in a safe order: an integer type with bool excluded, the minimum size, finite fractions inside the open interval, and the sum tolerance.
  • Partition sizes from the floor-plus-epsilon rule, with test taking the remainder and a check that no part is empty.
  • A single ordering (identity or seeded permutation) cut into consecutive blocks, so the parts are disjoint and together cover every index.
  • Sorted integer outputs and the time complexity.

Follow-up Questions Guidance

  • How would you change the function so each part keeps the class proportions of a label array?
  • When would a random three-way split give a misleading test score?
  • Why keep a separate test set when you already choose hyperparameters on a validation set?
  • When is k-fold cross-validation a better choice than one hold-out validation set?