Count Ordered Combination Sums with Negative 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 title sounds scarier than it is. It's Combination Sum IV with a length cap and negative numbers allowed. If you've got an OA invite and 48 hours, know the reduction: count ordered sequences by length, tracking the running sum. The negatives kill the usual 'sum only goes up' assumption, but the length cap saves you. If you freeze mid-assessment, StealthCoder sits invisibly on your screen as a safety net and reads the problem for you.
The problem
Given an array of distinct nonzero integers candidates, an integer target, and a positive integer maxLength, return the number of nonempty ordered sequences of at most maxLength values whose sum is target. You may reuse a candidate any number of times. Two sequences with the same values in different orders count separately. Sequences of different lengths count separately. Function countOrderedCombinationSumsWithNegatives(candidates: int[], target: int, maxLength: int) → int Examples Example 1 candidates = [1,-1] target = 0 maxLength = 2 return = 2 The valid nonempty sequences are [1,-1] and [-1,1]. The empty sequence is not counted. Example 2 candidates = [2,-1] target = 3 maxLength = 3 return = 3 The valid sequences are the three orderings of [2,2,-1]. Example 3 candidates = [-2,-1] target = -3 maxLength = 3 return = 3 The valid sequences are [-2,-1], [-1,-2], and [-1,-1,-1]. Constraints 1 <= candidates.length <= 30. -100 <= candidates[i] <= 100 and candidates[i] != 0. All values in candidates are distinct. -1000 <= target <= 1000. 1 <= maxLength <= 20. The correct answer fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that maxLength is at most 20 and each value is at most 100 in magnitude, so any prefix sum stays between -2000 and 2000. Define dp[len][sum] as the number of sequences of exactly len elements with that sum. Start with dp[0][0] = 1. For each len from 1 to maxLength, for each reachable sum, add every candidate. Answer is the sum of dp[len][target] for len from 1 to maxLength. Use an offset of 2000 for indexing, or a hash map per layer. The common pitfall is the classic 1D dp over target, which loops forever or breaks with negatives because there's no ordering guarantee. Another miss is counting the empty sequence when target is 0. Cost is about 20 x 4001 x 30, tiny. If you blank on the layered state during the live OA, StealthCoder can hand you this structure while the proctor sees nothing.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Ordered Combination Sums with Negative 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Ordered Combination Sums with Negative Values FAQ
What's the trick in this Pinterest OA problem?+
Add length to the state. Count sequences by exactly len elements and current sum, layer by layer. The length cap bounds the sum range, so negatives stop being a problem. Then total up dp[len][target] across all lengths from 1 to maxLength.
Why can't I use the normal Combination Sum IV dp?+
The standard 1D dp assumes sums only grow toward target. With negative candidates you can overshoot and come back, so there's no safe iteration order. You'd also ignore the maxLength limit. Layering by length fixes both issues cleanly.
What range of sums do I need to track?+
With maxLength 20 and values up to 100 in magnitude, any partial sum lies within -2000 to 2000. Use an offset of 2000 in a 2D array, or a map per layer. You can also prune sums that can't return to the target, but it's unnecessary.
How hard is this really?+
Medium. The idea is a simple layered dp, but the setup trips people up: negatives, the length cap, and excluding the empty sequence. If you've done Combination Sum IV, you're most of the way there. Test it on the three given examples.
How do I prepare in 48 hours?+
Write this dp from scratch twice. Check example 1 by hand, since it verifies that the empty sequence is excluded when the target is 0. Then review related ordered-count dp problems like climbing stairs variants. Focus on state design, not memorizing code.