FastPrepTrain-Test Split with a Seeded Shuffle
Problems/Train-Test Split with a Seeded Shuffle

Train-Test Split with a Seeded Shuffle

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Google SWE interview for ML teams reports an onsite request to write train-test-split with limits that were not stated. The report does not give the data format, ratio, randomness, stratification, or output. We chose a seeded shuffle of indices and an exact test count. The split goal matches the report. We do not claim these details were the interviewer's requirements.

You have n aligned dataset rows numbered from 0 to n - 1. Split their indices into training and test groups without reusing a row. The returned two-row array contains the training indices first and test indices second.

To make the split the same every time, initialize the index array in ascending order. Starting at its last position and moving left, advance a 32-bit unsigned state by state = (1664525 * state + 1013904223) mod 2^32; swap the index at the current position with the index at state mod (currentPosition + 1). Return the first n - testCount shuffled indices as training and the final testCount as test. Preserve their shuffled order within each group.

The indices can then be applied to features and labels together, keeping their rows aligned. This exercise does not request a stratified split or a particular ML library.

Function

splitTrainTestIndices(n: int, testCount: int, seed: int) → int[][]

Examples

Example 1

n = 5testCount = 2seed = 0return = [[4,0,1],[2,3]]

The seeded swaps give the order [4,0,1,2,3]. The last two indices form the test set.

Example 2

n = 4testCount = 1seed = 7return = [[1,3,0],[2]]

The shuffled order is [1,3,0,2], so the last index is held out.

Example 3

n = 3testCount = 2seed = 1return = [[2],[1,0]]

A two-row test set leaves one training row, with no repeated index.

Constraints

  • 2 <= n <= 10000.
  • 1 <= testCount < n.
  • 0 <= seed <= 2147483647.
  • Every input row index must occur exactly once across the two returned groups.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
  • Spiral MatrixAthenahealth · ONSITE INTERVIEW
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Tax Calculation with Year and Filing RulesGusto · ONSITE INTERVIEWGusto interview questions
  • Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
  • Deep Copy a Random-Pointer ListAmazon · ONSITE INTERVIEW
  • Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
  • Instant-Runoff Voting WinnerWise · ONSITE INTERVIEW
  • Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] splitTrainTestIndices(int n, int testCount, int seed) {
        // Write your code here.
    }
}
Checking account…