Combination Sum with Reusable Values
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Pinterest reported this one in August 2026, and the constraint that matters is target <= 40 with positive candidates. That rules out enumerating every multiset blindly, but it's small enough that backtracking with pruning finishes fast. It's Combination Sum with a lexicographic output requirement. If you've seen the classic, you're mostly there. If you haven't, the shape is simple once you see it. The OA wants every unique combination, nondecreasing, sorted. StealthCoder sits quietly on your desktop as a safety net if your mind goes blank mid-assessment, but the pattern below should get you most of the way.
The problem
Given an array of distinct positive integers candidates and a positive integer target, return every unique combination whose values sum to target. You may select the same candidate any number of times. Each combination must be nondecreasing. Return the combinations in lexicographic order. Function combinationSumReuse(candidates: int[], target: int) → int[][] Examples Example 1 candidates = [2,3,6,7] target = 7 return = [[2,2,3],[7]] The value 2 may be reused, giving [2,2,3]. The single value 7 is the other valid combination. Example 2 candidates = [2,3,5] target = 8 return = [[2,2,2,2],[2,3,3],[3,5]] All three nondecreasing combinations sum to 8 and are listed lexicographically. Example 3 candidates = [3,4] target = 2 return = [] Every candidate exceeds target, so no combination is possible. Constraints 1 <= candidates.length <= 30. 1 <= candidates[i] <= 40. All values in candidates are distinct. 1 <= target <= 40.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is backtracking over a sorted candidates array. Sort first. At each step, pick a start index, add candidates[i], and recurse with the same index i, since reuse is allowed. Never go backward, which keeps combinations nondecreasing and kills duplicates without a set. Because you iterate in sorted order and push in index order, the output comes out lexicographic for free. Prune hard: if candidates[i] exceeds the remaining target, break, since everything after is larger. The common pitfall is recursing with i+1, which forbids reuse, or recursing from 0, which creates permutations like [2,3,2]. Another is forgetting to copy the path when you record a result. Pop after each recursive call. With target at most 40 and smallest value 1, depth maxes at 40, which is fine. If you freeze during the live OA, StealthCoder can hand you the working template as a hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Combination Sum with Reusable Values cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as combination sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Combination Sum with Reusable Values FAQ
What's the trick in this Pinterest combination sum problem?+
Sort candidates, then backtrack with a start index. Recurse with the same index to allow reuse, never a lower one. That enforces nondecreasing order, prevents duplicate combinations, and produces lexicographic output automatically. Break the loop once a candidate exceeds the remaining target.
How hard is this really?+
Medium. It's the standard Combination Sum pattern with an ordering requirement that sorting already satisfies. If you can write a clean backtracking function with choose, recurse, undo, you can solve it. The small constraints mean no heavy optimization is needed.
Do I need dynamic programming here?+
No. DP can count ways to hit the target, but you need to return every actual combination. Backtracking generates them directly. With target at most 40, the number of results stays manageable, so plain recursion with pruning is the right tool.
Why does the output come out in lexicographic order?+
Because you sort candidates first and always try smaller values before larger ones at each position. Depth-first exploration in that order emits combinations in lexicographic order. You don't need a final sort, though adding one is harmless if you're nervous.
How do I prepare in 48 hours?+
Write this backtracking template from memory twice. Then trace Example 2 by hand to confirm [2,2,2,2], [2,3,3], [3,5] come out in order. Practice the edge case where every candidate exceeds target and you return an empty list. That covers nearly everything this problem tests.