KatInterview

Enumerate Every Settings Grid from Option Lists

Arrays & HashingAuto-graded · 18 test cases
EasyAuto-gradedArrays & Hashingcartesian-productbacktrackingitertools

Last updated

Build the Cartesian product of named option lists into a list of dictionaries, handling empty inputs and a defined output order.

Build a Full Settings Grid

You are given a dictionary that maps each setting name to a list of candidate values for that setting. Write a function that returns every possible full assignment of settings. Each assignment is a dictionary that picks exactly one candidate value for every name in the input.

Output order

The result must be in this exact order:

  • Process the names in the order they appear in the input dictionary.
  • The first name changes slowest and the last name changes fastest, as in nested loops where the first name is the outermost loop.
  • Within one name, candidate values are used in the order they appear in its list.

Each returned dictionary should list its keys in the same order as the input dictionary.

Special cases

  • If the input dictionary is empty, return a list containing a single empty dictionary: [{}].
  • If any name has an empty list of candidates, no full assignment exists, so return [].
  • Candidate values can be of any type (numbers, strings, None, and so on). Do not inspect or modify them.
  • Do not modify the input dictionary. Every returned dictionary must be a separate object.

Constraints

  • The input has at most 10 names.
  • Each candidate list has at most 5 values.
  • Candidate lists contain no repeated values.

Examples

Example 1

Input: build_search_grid({'learning_rate': [0.01, 0.1], 'batch_size': [32, 64]})

Output: [{'learning_rate': 0.01, 'batch_size': 32}, {'learning_rate': 0.01, 'batch_size': 64}, {'learning_rate': 0.1, 'batch_size': 32}, {'learning_rate': 0.1, 'batch_size': 64}]

Example 2

Input: build_search_grid({'optimizer': ['sgd', 'adam'], 'dropout': [0, 0.2], 'epochs': [10, 20]})

Output: [{'optimizer': 'sgd', 'dropout': 0, 'epochs': 10}, {'optimizer': 'sgd', 'dropout': 0, 'epochs': 20}, {'optimizer': 'sgd', 'dropout': 0.2, 'epochs': 10}, {'optimizer': 'sgd', 'dropout': 0.2, 'epochs': 20}, {'optimizer': 'adam', 'dropout': 0, 'epochs': 10}, {'optimizer': 'adam', 'dropout': 0, 'epochs': 20}, {'optimizer': 'adam', 'dropout': 0.2, 'epochs': 10}, {'optimizer': 'adam', 'dropout': 0.2, 'epochs': 20}]

Example 3

Input: build_search_grid({})

Output: [{}]

Hints

Hint 1

Each full assignment chooses one value from every list. This is the Cartesian product of the lists.

Hint 2

Fix an order for the keys, such as the dictionary's own order. Then either recurse over key positions, or use itertools.product on the value lists and zip each tuple back with the keys.

Hint 3

You do not need special handling for the empty cases. The product of zero lists yields one empty tuple, and a product containing an empty list yields nothing. If you use recursion, append a copy of the partial dictionary, not the shared one.