Seeded Hyperparameter Search Space Sampler
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `sample_configurations(schema, n_trials, seed)` for a random hyperparameter search. `schema` maps parameter names to spec dicts, each with a `"type"` key. Return a list of `n_trials` config dicts whose keys follow schema order. The same seed must always produce the same list.
Requirements:
- Use one `rng = np.random.default_rng(seed)`. Draw trial by trial, and within each trial draw parameters in schema order.
- `"uniform"`: `float(rng.uniform(low, high))`. `"loguniform"`: `exp(rng.uniform(log(low), log(high)))` as a `float`.
- `"int"`: `int(rng.integers(low, high + 1))`, so both bounds are inclusive. `"categorical"`: `choices[int(rng.integers(len(choices)))]`, returning the original object.
- Validate every spec before sampling and raise `ValueError` for: an unknown type, missing `low`/`high`, empty `choices`, non-integer int bounds, int `low > high`, float `low >= high`, or loguniform `low <= 0`.
- `n_trials < 0` raises `ValueError`. `n_trials == 0` returns `[]`.
Example:
```python
schema = {
"lr": {"type": "loguniform", "low": 1e-4, "high": 1e-1},
"depth": {"type": "int", "low": 2, "high": 8},
"act": {"type": "categorical", "choices": ["relu", "tanh"]},
}
sample_configurations(schema, 2, seed=7)
# -> [{"lr": <float>, "depth": <int in 2..8>, "act": "relu" | "tanh"}, {...}]
# Same seed -> identical list
```
```hint Order of operations
Ask what the generator and the caller have already seen if a bad spec only turns up halfway through the sampling loop. Also check whether `rng.integers` includes its upper argument.
```
### Constraints & Assumptions
- `schema` is a plain `dict`, so iteration follows insertion order.
- `choices` is an indexable sequence such as a list or tuple, and its items can be any objects.
- `seed` is anything `np.random.default_rng` accepts, usually an integer.
### Clarifying Questions to Ask
- Should a float with a whole value, such as `2.0`, count as an integer bound for an `"int"` parameter?
- Should the schema still be validated when `n_trials` is 0, or can the function return `[]` right away?
- Should extra keys in a spec (for example `"low"` on a categorical) be rejected or ignored?
### What a Strong Answer Covers
- Validation of the whole schema before any random draw, with a `ValueError` for each listed case.
- One generator and a fixed draw order (trial first, then parameters in schema order), which is what makes the output reproducible.
- Correct ranges per type: an inclusive integer upper bound, sampling in log space for `loguniform`, and the original object for categoricals.
- Conversion of NumPy scalars to native `float` and `int`.
- Time and space complexity, and the edge cases (`n_trials` of 0 or negative, an int parameter with `low == high`).
### Follow-up Questions
- Why sample a learning rate on a log scale, and what goes wrong with plain uniform sampling over `[1e-4, 1e-1]`?
- Adding a parameter to the middle of the schema changes every later draw for the same seed. How would you keep each parameter's values stable as the schema grows?
- How would you support conditional parameters, such as `momentum` only when `optimizer == "sgd"`?
- How would you add a `step` option that quantizes a `uniform` parameter, and how would you validate it?
Overview: Implement a seeded random sampler for hyperparameter search that draws uniform, log-uniform, integer and categorical values from a schema with NumPy. It tests up-front input validation, a reproducible draw order from a single generator, and the correct range for each parameter type.