PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Generate all hyperparameter combinations

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given several *groups* of hyperparameter choices for an ML experiment. Each group contains one hyperparameter name and a list of candidate values. Return **all possible configurations** (the Cartesian product across groups), where each configuration assigns **exactly one value** to each hyperparameter. ### Input - A list of `k` hyperparameter groups, e.g. - `[("lr", [0.1, 0.01]), ("batch", [16, 32, 64]), ("optimizer", ["sgd", "adam"])]` ### Output - A list of configurations, each configuration represented as a map/dictionary from name → chosen value. ### Constraints / expectations - If any group has an empty value list, the result should be empty. - Order does not matter, but the output should not contain duplicates. - Discuss a few test cases (including edge cases) you would use to validate the implementation.

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.

You are given several groups of hyperparameter choices for an ML experiment. Each group is a pair of (hyperparameter name, list of candidate values). Return ALL possible configurations — the Cartesian product across groups — where each configuration assigns exactly one value to each hyperparameter. Each configuration is a map from name to chosen value. Rules: - If any group has an empty value list, the result is empty (no configuration is possible). - If there are no groups, the result is a single empty configuration. - Output must not contain duplicate configurations (a group may list a value more than once). - Order of configurations does not matter; in the reference implementation the output is sorted canonically so results are deterministic. Example: Input: [("lr", [0.1, 0.01]), ("batch", [16, 32]), ("optimizer", ["sgd", "adam"])] Output: 8 configurations, one per element of the Cartesian product, each a dict like {"lr": 0.1, "batch": 16, "optimizer": "sgd"}. Note on the typed (Java/C++/JS) templates: values are modeled as strings so the structure is uniform and marshalable across languages; the Python reference works with native value types.

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

  1. 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.
  2. Short-circuit to an empty result as soon as you see a group whose value list is empty.
  3. To deduplicate configurations (when a group lists a value more than once), hash each config by its sorted (name, value) pairs.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)