Deterministic train/test split

A free medium coding practice problem from ml. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Medium
Type
Function
Topic
Ml
Languages
JavaScript, TypeScript or Python

Problem

Split a dataset into train/test by hashing each record id, so the split never changes between runs. Hash an id with 32-bit FNV-1a: start with h = 2166136261; for each character (ids are ASCII) set h = h XOR charCode, then h = (h × 16777619) mod 2^32. The record goes to the TEST set when h mod 100 < testPercent (0–100).

Return the ids in the test set, in input order (duplicates kept). For reference, the hash of "a" is 3826002220. Up to 100 000 ids. (Careful with 32-bit overflow: in JavaScript use Math.imul(...) >>> 0 or BigInt; in Python mask with & 0xFFFFFFFF.)

Function signature

// JavaScript
function testSplit(ids, testPercent)

// TypeScript
function testSplit(ids: any, testPercent: any): any

// Python
def test_split(ids, testPercent):

Examples

  1. testSplit(["a","b","c","d","e","f"], 50) → ["a","e","f"]
  2. testSplit(["user-1","user-2","user-3"], 100) → ["user-1","user-2","user-3"]

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps