Neural Mastery
← Practice
Deterministic Hash-Based Train/Test Splitmedium

Deterministic Hash-Based Train/Test Split

medium ML Systems & MLOps 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement hash-based deterministic train/test splitting, which stays stable as new data arrives (unlike a random shuffle-and-cut split that reshuffles everything on every re-run).

Task

Implement hash_split(record_id, test_pct). Hash record_id with hashlib.md5, take the first 4 bytes as a big-endian unsigned int, compute bucket = (that_int % 10000) / 100.0 (in [0,100)), and return 'test' if bucket < test_pct else 'train'.

Function Signature

hash_split(record_id: str, test_pct: float) -> str

Examples

Example 1: 0% test always train
Input: record_id = 'rec-1' test_pct = 0
Output: 'train'
Example 2: 100% test always test
Input: record_id = 'rec-1' test_pct = 100
Output: 'test'
Example 3: deterministic for same id
Input: record_id = 'rec-42' test_pct = 20
Output: 'train'

Constraints

  • 0 <= test_pct <= 100
Python3Saved ✓
def hash_split(record_id, test_pct):
# Your implementation here
pass

Case 1: 0% test always train
Input: record_id = 'rec-1' test_pct = 0
Expected: 'train'
Case 2: 100% test always test
Input: record_id = 'rec-1' test_pct = 100
Expected: 'test'
Case 3: deterministic for same id
Input: record_id = 'rec-42' test_pct = 20
Expected: 'train'