Train-Test Split with a Seeded Shuffle
Updated
Problem statement
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