Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Cross-Validation in Python
00:00
5 left

Cross-Validation in Python

MediumPython

Problem

H2O-3 evaluates models across multiple folds to estimate generalization performance. Implement the split-generation portion of k-fold cross-validation without using machine learning libraries.

Given a feature matrix X, label array y, number of folds k, and optional shuffling parameters, return the train and validation indices for every fold. Each sample must appear in exactly one validation set, while every training set contains all samples not assigned to that fold.

Formal Specification

Implement cross_validate(X, y, k, shuffle, seed):

  • X is a list of feature rows. Its values are not used when creating indices.
  • y is a list of labels with the same length as X.
  • k is an integer between 2 and len(X).
  • shuffle is a Boolean. If false, preserve the original index order. If true, shuffle indices using seed.
  • seed is an integer or None.
  • Return a list of k dictionaries. Each dictionary must contain train, a list of indices, and validation, a list of indices.

Validation folds should be as balanced as possible. Earlier folds receive one extra sample when the dataset size is not divisible by k. Do not mutate X or y.

Constraints

  • 2 <= len(X) == len(y) <= 10^5
  • 2 <= k <= len(X)
  • Fold sizes differ by at most one sample
  • Each index appears in exactly one validation fold
  • X and y must not be mutated

Function Signature

def cross_validate(X, y, k, shuffle, seed):
Interviewer

Your question is Cross-Validation in Python. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.