Conditional Hyperparameter Search Space with Validation and Seeded Sampling
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `SearchSpace` with `add_float`, `add_int`, `add_categorical`, `active_params(config)`, `validate(config)` and `sample(n, seed)`. A `condition` is a tuple `(parent_name, allowed_values)`: the parameter is active only when its parent is active and the parent's value is in `allowed_values`.
Requirements:
- Raise `ValueError` for: duplicate name, undefined parent, empty or out-of-domain condition values, float `low >= high`, `log=True` with `low <= 0`, int `low > high`, empty `choices`.
- `active_params` returns active names as a list in definition order.
- `validate` returns `True` only if the keys equal the active set exactly and every value is in domain: bounds are inclusive, int values must have an integer type (so `3.0` is rejected), bools are rejected for numeric parameters, floats must be finite, and categories are matched by equality.
- `sample` uses one `np.random.default_rng(seed)`, walks parameters in definition order, skips inactive ones (no draw), and returns a list of `n` dicts with keys in definition order.
- Draws: float `rng.uniform(low, high)` (log: `exp(rng.uniform(log(low), log(high)))`), int `int(rng.integers(low, high + 1))`, categorical `choices[int(rng.integers(len(choices)))]`.
Parents are always defined before children. Conditions may nest: a grandchild is inactive when any ancestor is inactive.
Example:
```python
s = SearchSpace()
s.add_float("C", 1e-3, 1e3, log=True)
s.add_categorical("kernel", ["linear", "rbf", "poly"])
s.add_float("gamma", 1e-4, 1e1, log=True, condition=("kernel", ["rbf", "poly"]))
s.add_int("degree", 2, 5, condition=("kernel", ["poly"]))
s.validate({"C": 1.0, "kernel": "linear"}) # True
s.validate({"C": 1.0, "kernel": "linear", "gamma": 0.1}) # False (inactive key)
s.validate({"C": 1.0, "kernel": "rbf"}) # False (missing gamma)
```
```hint Resolving activity
Because every parent is defined before its children, a parent's activity is already settled by the time you reach the child. Think about what you need to carry along a single pass in definition order so that nested conditions come out right.
```
```hint Python's type hierarchy
`True` is an instance of `int` and of `numbers.Real`. Decide where the bool check has to sit so it rejects booleans for numeric parameters without breaking a categorical whose choices are booleans.
```
### What a Strong Answer Covers
- Registration checks that raise `ValueError` for every listed case, including condition values checked against the parent's own domain.
- Activity resolved in one ordered pass, where a parameter is active only when its parent is active and holds an allowed value, so grandchildren follow their ancestors.
- `validate` comparing the key set with the active set exactly and applying the type rules: inclusive bounds, integer types for ints, no bools for numeric parameters, finite floats.
- `sample` drawing from one generator, only for active parameters and in definition order, with the specified draw calls, so a seed always reproduces the same configs.
- The cost of sampling and validation in terms of the number of parameters.
### Follow-up Questions
- How would you support conditions combining multiple parents (AND/OR)?
- Why does skipping draws for inactive parameters matter for reproducibility?
- How would you encode these configs as fixed-length vectors for a Bayesian optimizer?
Overview: Build a Python hyperparameter search space where some parameters exist only when a parent takes certain values, with config validation and reproducible seeded sampling. It tests careful type checks, activity rules for nested conditions, and how random draw order affects reproducibility.