KatInterview

Generate Cartesian Product of Search Space Parameters

Arrays & HashingAuto-graded · 20 test cases
EasyAuto-gradedArrays & Hashingcartesian-productgeneratorarrays

Last updated

Construct all explicit configurations from a dictionary mapping parameter names to lists of candidate values using Cartesian product generation.

Given a dictionary grid where each key is a parameter name and its corresponding value is a list of candidate settings for that parameter, construct and return a list of dictionary configurations representing every possible parameter combination.

Each returned dictionary should map every key from grid to a single value selected from its list of candidates. The sequence of candidate choices should follow Cartesian product ordering, where values belonging to the rightmost keys vary the fastest (matching the order of nested iteration across the dictionary's keys in insertion order).

Special Rules

  • If grid is empty ({}), return a list containing a single empty dictionary: [{}].
  • If any key maps to an empty list ([]), no valid combination can be formed, so return an empty list ([]).
  • The order of keys in each configuration dictionary must match the insertion order of keys in the input dictionary.
  • Duplicate entries within candidate lists should be preserved as separate distinct configurations.

Constraints

  • The number of keys in grid is between 0 and 10.
  • Each candidate list contains between 0 and 10 elements.
  • Parameter names are non-empty strings.
  • Values can be of any data type (e.g., integers, floats, strings, standard Python objects).

Examples

Example 1

Input: generate_hyperparameter_combinations({'learning_rate': [0.1, 0.2], 'feature': ['A', 'B'], 'batch': [10, 20]})

Output: [{'learning_rate': 0.1, 'feature': 'A', 'batch': 10}, {'learning_rate': 0.1, 'feature': 'A', 'batch': 20}, {'learning_rate': 0.1, 'feature': 'B', 'batch': 10}, {'learning_rate': 0.1, 'feature': 'B', 'batch': 20}, {'learning_rate': 0.2, 'feature': 'A', 'batch': 10}, {'learning_rate': 0.2, 'feature': 'A', 'batch': 20}, {'learning_rate': 0.2, 'feature': 'B', 'batch': 10}, {'learning_rate': 0.2, 'feature': 'B', 'batch': 20}]

Example 2

Input: generate_hyperparameter_combinations({'optimizer': ['sgd', 'adam'], 'epochs': [10, 50]})

Output: [{'optimizer': 'sgd', 'epochs': 10}, {'optimizer': 'sgd', 'epochs': 50}, {'optimizer': 'adam', 'epochs': 10}, {'optimizer': 'adam', 'epochs': 50}]

Example 3

Input: generate_hyperparameter_combinations({'seed': [1]})

Output: [{'seed': 1}]

Example 4

Input: generate_hyperparameter_combinations({})

Output: [{}]

Hints

Hint 1

Think of this task as computing a Cartesian product over the sequences of candidate values associated with each parameter key.

Hint 2

The `itertools.product` module in Python provides a convenient standard library tool to generate Cartesian combinations in lexicographical order.

Hint 3

Combine key names with each generated tuple of values using `zip(keys, values)` to form individual candidate dictionaries.