The Combinations interview question is the most basic form of the combination problem. Given two integers n and k, you need to return all possible combinations of k numbers chosen from the range [1, n]. Unlike the "Combination Sum" series, there is no target sum—only a target count.
Apple and Adobe ask this Backtracking interview pattern to test fundamental recursion skills. It’s a building block for more complex problems. It checks if you understand the "base case" (when the combination size is k) and how to maintain the "only increasing" order to avoid duplicates like [1, 2] and [2, 1].
This is a pure Backtracking problem.
current list and a start index.start to n.i to current, then recursively call with start = i + 1.i (backtrack) and continue the loop.n = 4, k = 2
1.
1. Current = [1].2.2. Current = [1, 2]. (Size 2 reached! Add to result).[1].3. Current = [1, 3]. (Size 2! Add).[1].4. Current = [1, 4]. (Size 2! Add).2.
2. Current = [2].3.3. Current = [2, 3]. (Size 2! Add).
...and so on.[2, 1] in addition to [1, 2].n when there aren't enough numbers left to reach size k. (A common optimization is looping only up to n - (k - current.size()) + 1).current list before adding it to the final results list.Always remember to "copy" the current path when you reach a valid result. In many languages, if you just add the reference, your final result list will be full of empty lists (because you backtracked everything to empty).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Factor Combinations | Medium | Solve | |
| N-Queens II | Hard | Solve | |
| Tiling a Rectangle with the Fewest Squares | Hard | Solve | |
| The k-th Lexicographical String of All Happy Strings of Length n | Medium | Solve | |
| Combination Sum III | Medium | Solve |