Generate Attribute Combinations
PinterestSoftware Engineer · Technical ScreenLast updated
Explain Cartesian-product generation for attribute choices while preserving mixed value types and making ordering, duplicates, and empty cases explicit.
Generate Attribute Combinations
The source asks you to generate every combination from an attribute map and gives attribute0: [0, 1, 2] and attribute1: ["a", "b"]. Notice that the preserved values have different types. Explain a Cartesian-product algorithm that retains each original value and discuss the input-order, empty-input, duplicate-value, and output-order rules that must be clarified before implementation.
Constraints & Assumptions
- Integer values must remain integers and string values must remain strings.
- The source demonstrates two attributes but does not define a general cross-language value union or canonical map order.
- For the worked example only, process
attribute0beforeattribute1and preserve each listed value order. - Behavior for an empty attribute map or an attribute with no values must be agreed rather than inferred.
Clarifying Questions to Ask
- Which value types are supported, and must nested values be copied or referenced?
- Does attribute iteration follow insertion order, sorted names, or an explicit key list?
- Should duplicate input values produce duplicate output maps?
- What should empty input and an empty value list return?
Hints
Hint 1
Extend partial assignments: Start with one empty partial map. For each attribute in the confirmed key order, copy every current partial assignment once for each of that attribute's values.
Follow-up questions
- How would you stream combinations when materializing the entire result is too large?
- How can a caller request a canonical order without relying on map insertion order?
- What copying policy is needed if values can be mutable objects?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.