Successive Halving From Scratch
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `successive_halving(configs, objective, min_resource, max_resource, eta=3)`. `objective(config, resource)` returns a loss (lower is better). Return a dict with `best_index`, `best_config`, `rungs` (list of `{"resource", "indices", "losses"}`) and `total_resource`.
Requirements:
- Rung `k` uses `min_resource * eta**k` for every value `<= max_resource`.
- Evaluate each survivor exactly once per rung, in ascending original index order (a single survivor is still evaluated at later rungs).
- After every rung but the last, keep the `max(1, n // eta)` lowest losses (ties: lowest index), listed in ascending index order.
- Best = lowest loss at the last rung (ties: lowest index). `total_resource` sums the resource of every evaluation.
- Raise `ValueError` if `configs` is empty, `eta < 2`, `min_resource <= 0`, or `min_resource > max_resource`.
Example:
```python
configs = [{"x": float(i)} for i in range(10)]
objective = lambda c, r: (c["x"] - 4.2) ** 2 + 1.0 / r
out = successive_halving(configs, objective, 1, 10, eta=3)
# resources: [1, 3, 9] (27 > 10 is skipped)
# indices: [[0..9], [3, 4, 5], [4]]
# best_index = 4, total_resource = 10*1 + 3*3 + 1*9 = 28
```
```hint Build the schedule first
Compute the full list of rung resources before evaluating anything. It tells you which rung is the last one, the only rung where nobody is cut.
```
```hint One sort key
A single sort key can apply both the loss ranking and the tie rule. The kept set then has to go back to index order before the next rung.
```
### What a Strong Answer Covers
- The rung schedule from `min_resource * eta**k` up to and including `max_resource`, and how the code knows which rung is last.
- The evaluation order, the per-rung record, and how `total_resource` is accumulated.
- The keep rule `max(1, n // eta)` with ties to the lowest index and survivors returned to index order.
- Choosing the winner from last-rung losses only, plus the input validation.
- The number of objective calls and the edge cases (a lone survivor, a schedule with one rung).
### Follow-up Questions
- How does Hyperband build on this routine?
- How would you avoid retraining survivors from scratch at each rung?
- How would you parallelize this without waiting for full rungs?
Overview: Implement successive halving for hyperparameter search: evaluate configurations on a geometric resource schedule and keep the best fraction at each rung. It tests the rung schedule, deterministic tie-breaking, resource accounting, and how the method relates to Hyperband and ASHA.