Implement a Multi-Class Random Oversampler in NumPy
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `RandomOverSampler.fit_resample(X, y)`. It duplicates rows of every minority class, sampling with replacement, until each class has the majority class's count. Return `(X_resampled, y_resampled)` and store the selected row indices in `self.sample_indices_`.
The output must be reproducible for a given seed, so follow these rules:
- Create one generator, `np.random.default_rng(self.random_state)`.
- Output starts with all original rows in order (indices `0..n-1`), followed by the new samples.
- Visit classes in sorted order (`np.unique`). For each class with `need = n_max - n_c > 0`, draw `rng.integers(0, n_c, size=need)` and use these as positions into that class's row indices (in original order).
- Classes already at `n_max` make no draws, so they do not consume the generator.
- `sample_indices_` is an integer array satisfying `X_res == X[sample_indices_]`. Labels may be ints or strings.
Example:
```python
y = [0, 1, 1, 2, 2, 2, 2, 0, 1, 2] # counts: 0→2, 1→3, 2→5
# sample_indices_ = [0..9] + [0,7][rng.integers(0,2,size=3)]
# + [1,2,8][rng.integers(0,3,size=2)]
# len(y_res) == 15, every class count == 5
```
Class 2 is the majority with 5 rows. Class 0 needs 3 new rows drawn from its rows `[0, 7]`, class 1 needs 2 new rows drawn from `[1, 2, 8]`, and class 2 makes no draw.
```hint Positions versus row numbers
`rng.integers(0, n_c, size=need)` returns positions inside one class's list of rows, not row numbers of `X`. Work out how to turn those positions into rows of the full array before you index anything.
```
### What a Strong Answer Covers
- Getting the classes in sorted label order with their counts, and the target count `n_max`.
- Building one integer index array that starts with the original rows and appends each minority class's draws, mapped through that class's row indices in original order.
- Using a single shared generator and making no draw for classes already at `n_max`, so the indices match the spec for a given seed.
- Returning `X[indices]` and `y[indices]` with `sample_indices_` stored, and handling string labels.
- The time cost and the edge cases (a single class, already balanced data, empty input).
### Follow-up Questions
- How would you add a `sampling_strategy` parameter that sets a per-class target count or a ratio of the majority count?
- Why must oversampling run only on the training fold inside cross-validation, and what happens to the validation score if you resample before splitting?
- When would you choose SMOTE or class weights over duplicating rows, and what overfitting risk does duplication carry?
- How would you group the row indices by class without scanning `y` once per class?
Overview: Implement fit_resample for a multi-class random oversampler that duplicates minority-class rows with replacement until every class matches the majority count. It tests NumPy fancy indexing, reproducible draws from one seeded generator, and keeping features and labels aligned.