Constrained Parameter Grid Generator

Quick Overview

Write a lazy Python generator that walks every configuration of a hyperparameter grid in sorted-key order, deduplicates values by equality, drops inactive parameters and filters with a constraint function. It tests odometer-style iteration, edge cases such as empty spaces, and how to count feasible configurations.

Constrained Parameter Grid Generator

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given `space` (a dict mapping parameter names to lists of candidate values) and an optional `constraint(cfg) -> bool`, implement `iter_grid(space, constraint=None)` to lazily yield every feasible configuration as a dict. Implement `count_feasible(space, constraint=None)` to return how many configurations `iter_grid` would yield. Requirements: - Order: keys sorted ascending, the last key varies fastest (matches `sklearn` `ParameterGrid`). The constraint only filters and never reorders. - Duplicate values per key (by `==`, so `1` and `1.0` collide) collapse, keeping the first occurrence and its type. - A `None` value marks the parameter inactive: omit that key from the config. `constraint` receives a copy of the config with `None` keys already removed. - An empty `space` yields exactly `[{}]` (the empty config still goes through `constraint`). Any key with an empty value list yields nothing. - Each yielded dict is a fresh, independent object. Generation must be lazy. Example: ```python space = {"kernel": ["linear", "rbf"], "gamma": [None, 0.1, 1.0]} valid = lambda c: ("gamma" in c) == (c["kernel"] == "rbf") list(iter_grid(space, valid)) # [{'kernel': 'linear'}, # {'gamma': 0.1, 'kernel': 'rbf'}, # {'gamma': 1.0, 'kernel': 'rbf'}] count_feasible(space, valid) # 3 ``` ```hint State between yields Without `itertools.product`, ask what the smallest amount of state is that tells you which configuration comes next. It does not need to grow with the size of the grid. ``` ```hint Deduplication pitfalls Values may be unhashable, so a set is not an option. Check what `1 == 1.0` and `True == 1` return before deciding how to compare. ``` ### Constraints & Assumptions - The full grid may have ${10^{12}}$ points or more (for example 6 keys with 100 values each). Producing the first item must be immediate. - Values need not be hashable, only comparable with `==`. - Do not use `itertools.product` or scikit-learn. ### What a Strong Answer Covers - Yields configs in sorted-key order with the last key varying fastest, keeping only a small per-key index as state so the first item appears immediately. - Deduplicates each value list with `==` in a way that works for unhashable values and keeps the first occurrence and its type. - Treats `None` as inactive, hands the constraint a copy without those keys, and builds a new dict for every yield. - Handles an empty `space` and a key with an empty value list correctly. - Explains what `count_feasible` costs and when the count can be computed without enumerating the grid. ### Follow-up Questions - How would you count feasible configs without enumerating a huge grid? - How would you support truly conditional spaces (for example, `gamma` only exists under `rbf`)? - How would you sample uniformly from the feasible set instead of enumerating it?

Overview: Write a lazy Python generator that walks every configuration of a hyperparameter grid in sorted-key order, deduplicates values by equality, drops inactive parameters and filters with a constraint function. It tests odometer-style iteration, edge cases such as empty spaces, and how to count feasible configurations.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given space (a dict mapping parameter names to lists of candidate values) and an optional constraint(cfg) -> bool, implement iter_grid(space, constraint=None) to lazily yield every feasible configuration as a dict. Implement count_feasible(space, constraint=None) to return how many configurations iter_grid would yield.

Requirements:

  • Order: keys sorted ascending, the last key varies fastest (matches sklearn ParameterGrid ). The constraint only filters and never reorders.
  • Duplicate values per key (by == , so 1 and 1.0 collide) collapse, keeping the first occurrence and its type.
  • A None value marks the parameter inactive: omit that key from the config. constraint receives a copy of the config with None keys already removed.
  • An empty space yields exactly [{}] (the empty config still goes through constraint ). Any key with an empty value list yields nothing.
  • Each yielded dict is a fresh, independent object. Generation must be lazy.

Example:

space = {"kernel": ["linear", "rbf"], "gamma": [None, 0.1, 1.0]}
valid = lambda c: ("gamma" in c) == (c["kernel"] == "rbf")
list(iter_grid(space, valid))
# [{'kernel': 'linear'},
#  {'gamma': 0.1, 'kernel': 'rbf'},
#  {'gamma': 1.0, 'kernel': 'rbf'}]
count_feasible(space, valid)  # 3

Constraints & Assumptions

  • The full grid may have 1012{10^{12}} points or more (for example 6 keys with 100 values each). Producing the first item must be immediate.
  • Values need not be hashable, only comparable with == .
  • Do not use itertools.product or scikit-learn.

What a Strong Answer Covers Guidance

  • Yields configs in sorted-key order with the last key varying fastest, keeping only a small per-key index as state so the first item appears immediately.
  • Deduplicates each value list with == in a way that works for unhashable values and keeps the first occurrence and its type.
  • Treats None as inactive, hands the constraint a copy without those keys, and builds a new dict for every yield.
  • Handles an empty space and a key with an empty value list correctly.
  • Explains what count_feasible costs and when the count can be computed without enumerating the grid.

Follow-up Questions Guidance

  • How would you count feasible configs without enumerating a huge grid?
  • How would you support truly conditional spaces (for example, gamma only exists under rbf )?
  • How would you sample uniformly from the feasible set instead of enumerating it?