The Factor Combinations interview question (also known as "Get Factors") asks you to find all possible combinations of factors that multiply to a given integer n. The factors must be greater than 1 and less than n. For example, for , the valid combinations are [2, 2, 3], [2, 6], [3, 4]. Note that the order of factors in a combination does not matter, so [2, 6] and [6, 2] are considered the same.
Companies like Uber and LinkedIn use this Backtracking interview pattern to evaluate your ability to explore a state space recursively while avoiding duplicates. It’s a mathematical variation of the "Combination Sum" problem. It tests whether you can implement pruning (stopping the search when the product exceeds n) and how you maintain a non-decreasing order of factors to ensure uniqueness in the result set.
The primary pattern is Backtracking with a non-decreasing constraint.
backtrack(target, start_factor).start_factor up to sqrt(target).target % i == 0:
i to the current combination.backtrack(target / i, i). The start_factor for the next call is i to maintain order.target / i as a combination (to finish the current branch).i.
[2].target = 4, start = 2.
[2, 2].[2, 2, 2].[2, 4].
Result: [[2, 2, 2], [2, 4]].[2, 4] and [4, 2]. This is usually prevented by passing the current factor as the minimum for the next recursive step.sqrt(target), which leads to redundant checks.Whenever you need to find unique combinations where order doesn't matter, the "non-decreasing order" rule is the most standard and efficient way to handle de-duplication in backtracking.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Combinations | Medium | Solve | |
| N-Queens II | Hard | Solve | |
| Tiling a Rectangle with the Fewest Squares | Hard | Solve | |
| Combination Sum II | Medium | Solve | |
| Permutations | Medium | Solve |