Task 4 — Generic Combination Generator
Objective
Create a generic function that generates every possible combination by selecting exactly one value from each provided list.
The function must work with an arbitrary number of input lists and an arbitrary number of values inside each list.
This operation is the Cartesian product of the input collections.
Input
Represent the source lists as:
[][]int
For example:
lists := [][]int{
{1, 2, 3},
{3, 5, 7},
}
Result
For the provided example, the function must generate:
[][]int{
{1, 3},
{1, 5},
{1, 7},
{2, 3},
{2, 5},
{2, 7},
{3, 3},
{3, 5},
{3, 7},
}
Combination Rule
Every result contains exactly one value from every input list.
For:
List 1 = {a, b}
List 2 = {c, d}
the result is:
{a, c}
{a, d}
{b, c}
{b, d}
Example 1
Input:
[][]int{
{1, 2, 3},
{3, 5, 7},
}
Expected result:
[][]int{
{1, 3},
{1, 5},
{1, 7},
{2, 3},
{2, 5},
{2, 7},
{3, 3},
{3, 5},
{3, 7},
}
The number of combinations is:
3 * 3 = 9
Example 2
Input:
[][]int{
{1, 2},
{3, 5},
{8, 9},
}
Expected result:
[][]int{
{1, 3, 8},
{1, 3, 9},
{1, 5, 8},
{1, 5, 9},
{2, 3, 8},
{2, 3, 9},
{2, 5, 8},
{2, 5, 9},
}
The number of combinations is:
2 * 2 * 2 = 8
Additional Input Shapes
The same function must also support structures such as:
Example 3
List 1: 1|2
List 2: 3|5|7
List 3: 4|6|8|9
List 4: 10|11
List 5: 14|15
and:
Example 4
List 1: 1|2|3|4
List 2: 5|6|7
List 3: 8|9
List 4: 10|11|12|13
as well as any other valid number of input lists.
Generic Requirement
The implementation must not contain fixed logic such as:
if two lists -> implementation A
if three lists -> implementation B
if four lists -> implementation C
One algorithm must solve all cases.
Conceptually:
func CreateCombinations(lists [][]int) [][]int
is sufficient as an API shape.
Number of Results
If the input contains lists with lengths:
L1, L2, L3, ..., Ln
the number of generated combinations is:
L1 * L2 * L3 * ... * Ln
For example:
2 * 3 * 4 * 2 * 2 = 96
possible combinations.
Empty Inner List
If any selected input list is empty:
[][]int{
{1, 2},
{},
{8, 9},
}
there are no valid combinations.
The result should therefore be:
[][]int{}
Empty Outer List
An empty outer collection does not provide any values from which a combination can be created.
For this exercise, it should return:
[][]int{}
Result Ordering
For deterministic output, combinations should follow input order.
The first list changes slowest.
The last list changes fastest.
For example:
{1,3,8}
{1,3,9}
{1,5,8}
{1,5,9}
{2,3,8}
...
Requirements
The function must:
- accept any number of integer lists
- accept different list lengths
- select exactly one value from every list
- generate every possible combination
- avoid generating duplicate combinations caused by the algorithm itself
- preserve deterministic traversal order
- use one generic implementation
Duplicate Source Values
If an input list itself contains duplicate values, duplicate result values may naturally be generated.
For example:
[][]int{
{1, 1},
{2},
}
logically contains two source positions and therefore produces:
[][]int{
{1, 2},
{1, 2},
}
unless the implementation explicitly normalizes duplicate source values.
The original task does not require source deduplication.
Implementation Notes
This problem can be solved using:
- recursion
- backtracking
- iterative Cartesian-product expansion
The important requirement is that the solution remains generic.
Conceptually:
choose one value from list 0
↓
choose one value from list 1
↓
...
↓
choose one value from final list
↓
emit combination
The implementation should not depend on the examples shown in the task.