Count Ordered Combination Sums
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Pinterest OA, reported in August 2026, is looping candidates on the outside and getting combinations instead of ordered sequences. Count Ordered Combination Sums looks like the classic coin change setup, but [1,2] and [2,1] must count separately. That one detail flips your loop order. It's a dynamic programming problem with a tiny state, and the code is about eight lines once you see it. If you blank under the timer, StealthCoder runs invisibly during the live assessment and can hand you the structure so you don't ship the wrong loop order.
The problem
Given an array of distinct positive integers candidates and a positive integer target, return the number of nonempty ordered sequences whose values sum to target. You may reuse a candidate any number of times. Two sequences with the same values in different orders count separately. Function countOrderedCombinationSums(candidates: int[], target: int) → int Examples Example 1 candidates = [1,2,3] target = 4 return = 7 The sequences are [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3], and [3,1]. Example 2 candidates = [2,4] target = 6 return = 3 The valid ordered sequences are [2,2,2], [2,4], and [4,2]. Example 3 candidates = [5] target = 3 return = 0 No sequence of reusable 5s can sum to 3. Constraints 1 <= candidates.length <= 200. 1 <= candidates[i] <= 1000. All values in candidates are distinct. 1 <= target <= 1000. The correct answer fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Define dp[t] as the number of ordered sequences summing to t, with dp[0] = 1 as the empty sequence. For each t from 1 to target, loop over every candidate c where c <= t and add dp[t - c]. The target loop sits outside and the candidates loop sits inside. Swap them and you count unordered combinations, which gives 4 on example 1 instead of 7. Complexity is O(target * n), at most 200,000 operations here, so no pruning is needed. The empty sequence at dp[0] is only a base case, and the answer dp[target] never includes it since target is at least 1. Overflow is not a concern because the answer fits in 32 bits, though intermediate unreachable values can be large in other languages. Test example 2 by hand: dp[2]=1, dp[4]=2, dp[6]=3. If your brain freezes during the live OA, StealthCoder is the safety net that surfaces this recurrence fast.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Count Ordered Combination Sums 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as combination sum iv. 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Ordered Combination Sums FAQ
What's the trick in Count Ordered Combination Sums?+
Put the target loop on the outside and the candidates loop on the inside. That order counts each permutation separately. dp[t] equals the sum of dp[t - c] over all candidates c that fit. Base case is dp[0] = 1.
How hard is this Pinterest OA question really?+
Easy to medium. It's a one-dimensional DP with a short recurrence. The difficulty is purely in loop order. If you've seen coin change, you can finish it in minutes. If you haven't, the example with 7 sequences guides you.
Why does swapping the loops give the wrong answer?+
Candidates on the outside means each coin type is processed once in a fixed order, so you only count multisets. [1,2] and [2,1] collapse into one. Target on the outside lets any candidate be the last element at every sum, which counts orderings.
What's the time and space complexity?+
Time is O(target * n), at most about 200,000 steps with the given constraints. Space is O(target) for the dp array. Nothing fancier is needed, and sorting candidates only helps if you want to break early when c exceeds t.
How do I prepare for this in 48 hours?+
Write the dp from scratch twice. Run it by hand on candidates [1,2,3] with target 4 and confirm you get 7. Then run [2,4] with target 6 for 3. Also check the no-solution case, like [5] with target 3, returning 0.