Duplicate-Aware Group K-Fold Splitting
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `duplicate_group_ids(X, decimals=6)`, `group_kfold_splits(groups, n_splits)` and `dedup_aware_kfold(X, n_splits, decimals=6)` so that exact and near-duplicate rows always land in the same validation fold. The splitting functions return a list of `n_splits` tuples `(train_idx, val_idx)`.
The functions must meet these requirements:
- `duplicate_group_ids`: rows identical after `np.round(X, decimals)` share an `int64` ID. IDs are 0, 1, 2, ... in order of first appearance, `-0.0` equals `0.0`, and a 1-D `X` is treated as one column.
- `group_kfold_splits`: process groups by size descending (ties: earlier first appearance first), assigning each to the fold with the smallest current sample count (ties: lowest fold index).
- Fold `f`'s `val_idx` is all samples assigned to fold `f`, and `train_idx` is the rest. Both are sorted ascending.
- Raise `ValueError` if `n_splits < 2` or `n_splits` exceeds the number of distinct groups.
- `dedup_aware_kfold` is `group_kfold_splits` applied to `duplicate_group_ids(X, decimals)`.
Example:
```python
duplicate_group_ids([[1.0, 2.0], [3.0, 4.0], [1.0000001, 2.0],
[5.0, 6.0], [3.0, 4.0], [1.0, 2.1]], decimals=6)
# -> [0, 1, 0, 2, 1, 3]
group_kfold_splits([2, 0, 1, 0, 3, 1, 0, 4, 3], 3)
# val_idx per fold -> [1, 3, 6], [0, 2, 5], [4, 7, 8]
```
```hint Keys for rounded rows
Decide what hashable value represents one rounded row and how to hand out IDs in first-appearance order. Then check what rounding does to a tiny negative number such as -1e-9.
```
```hint Encoding the tie rules
The output only matches when both orderings are exact. Put the size and the first-appearance position into one sort key, and check how your fold-picking step breaks ties between equal loads.
```
### What a Strong Answer Covers
- A hashable key per rounded row, with IDs in first-appearance order and `-0.0` treated as `0.0`.
- Assignment of whole groups to folds, with the size-descending order and both tie rules implemented exactly.
- Validation of `n_splits` against the number of distinct groups, and correct handling of 1-D input.
- Sorted train and validation indices, with no group appearing on both sides of any split.
- Time and space complexity, and the limits of rounding as a near-duplicate test.
### Follow-up Questions
- Rounding misses near-duplicates that straddle a rounding boundary. How would you fix that?
- One group holds 40% of the rows and `n_splits` is 5. What happens to fold balance, and what would you do about it?
- How would you also keep the class ratio similar across folds while keeping every group intact?
- How would you add seeded shuffling so that different seeds give different but reproducible splits?
Overview: Implement duplicate-aware group k-fold cross-validation in NumPy. Rows that match after rounding share a group ID, and whole groups are assigned to folds greedily by size with exact tie rules. It tests leakage prevention, deterministic tie-breaking and careful handling of floating-point keys.