Generate all hyperparameter combinations
Company: Pinterest
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates competence in combinatorial enumeration and manipulation of data structures for generating hyperparameter configurations, testing understanding of Cartesian products and correctness of produced configuration sets.
Constraints
- 0 <= number of groups (k)
- Each group has a unique name and a list of candidate values (the list may contain duplicates).
- If any value list is empty, the result is empty.
- With no groups, the result is a single empty configuration [{}].
- Output contains no duplicate configurations.
Examples
Input: ([("lr", [0.1, 0.01]), ("batch", [16, 32]), ("optimizer", ["sgd", "adam"])],)
Expected Output: [{'batch': 16, 'lr': 0.01, 'optimizer': 'adam'}, {'batch': 16, 'lr': 0.01, 'optimizer': 'sgd'}, {'batch': 16, 'lr': 0.1, 'optimizer': 'adam'}, {'batch': 16, 'lr': 0.1, 'optimizer': 'sgd'}, {'batch': 32, 'lr': 0.01, 'optimizer': 'adam'}, {'batch': 32, 'lr': 0.01, 'optimizer': 'sgd'}, {'batch': 32, 'lr': 0.1, 'optimizer': 'adam'}, {'batch': 32, 'lr': 0.1, 'optimizer': 'sgd'}]
Explanation: Full Cartesian product of 2 x 2 x 2 = 8 configurations.
Input: ([("lr", [0.1]), ("batch", [])],)
Expected Output: []
Explanation: The 'batch' group has an empty value list, so no configuration is possible.
Input: ([],)
Expected Output: [{}]
Explanation: With no groups there is exactly one configuration: the empty assignment.
Input: ([("lr", [0.1, 0.01, 0.001])],)
Expected Output: [{'lr': 0.001}, {'lr': 0.01}, {'lr': 0.1}]
Explanation: A single group yields one configuration per candidate value.
Input: ([("a", [1, 1, 2]), ("b", ["x"])],)
Expected Output: [{'a': 1, 'b': 'x'}, {'a': 2, 'b': 'x'}]
Explanation: The duplicate value 1 in group 'a' is collapsed, so only two distinct configurations remain.
Hints
- Build the product incrementally: start from a single empty configuration and, for each group, extend every partial configuration with each candidate value of that group.
- Short-circuit to an empty result as soon as you see a group whose value list is empty.
- To deduplicate configurations (when a group lists a value more than once), hash each config by its sorted (name, value) pairs.