Subset Sum to Target
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs reportedly asked this one in September 2026, and the constraints are the whole story. Subset Sum to Target looks like a textbook DFS or knapsack, but the values run up to 10^9 and can be negative, so the usual DP table is dead on arrival. Array length caps at 30, which is the real hint. If you've got an OA invite for this, you need the one trick that fits the numbers. And if you blank mid-assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the approach while you keep typing.
The problem
You are given an array nums of signed integers and a signed integer target. Determine whether some subset of array indices has values summing exactly to target. Each index may be selected at most once, and the empty subset is allowed. Return 1 if such a subset exists; otherwise return 0. Function hasSubsetSum(nums: int[], target: int) → int Examples Example 1 nums = [3,34,4,12,5,2] target = 9 return = 1 Selecting 4 and 5 reaches the target 9. Example 2 nums = [2,-4,7] target = 1 return = 0 The possible non-empty sums are 2, -4, 7, -2, 9, 3, and 5, so no subset reaches 1. Constraints 0 <= nums.length <= 30. -10^9 <= nums[i], target <= 10^9. Use 64-bit arithmetic for intermediate subset sums.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Brute force over all subsets is 2^30, about a billion, which is borderline and risky. The standard DP over sums fails because values reach 10^9 and go negative, so you can't index by sum. The fix is meet in the middle. Split nums into two halves of at most 15. Enumerate all subset sums of each half, about 32768 each. Sort one list, then for every sum in the other, binary search or two-pointer for target minus that sum. That's roughly 2^15 times 15 work. Pitfalls: forgetting the empty subset, which means a target of 0 always returns 1. Also overflow, so use 64-bit sums. Empty array should return 1 only if target is 0. A hash set of one half's sums also works and skips the sort. If the live OA freezes you on the split idea, StealthCoder is the hedge that surfaces it fast.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Subset Sum to Target 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Subset Sum to Target FAQ
What's the trick in Subset Sum to Target?+
Meet in the middle. Length is at most 30, so split the array into two halves, generate every subset sum for each half, then look for a pair summing to target. It cuts 2^30 work down to about 2^15 per half plus a lookup.
Why doesn't classic knapsack DP work here?+
DP indexes by sum, and values go up to 10^9 in magnitude and can be negative. The table would be enormous and need offsets for negatives. The small length of 30 is the signal to enumerate subsets instead of indexing sums.
Is plain DFS enough for the Goldman Sachs OA?+
Plain include/exclude DFS is 2^30 calls in the worst case, which is around a billion. It might pass small tests and time out on large ones. Pruning doesn't help much with negatives, so meet in the middle is the safe route.
What edge cases should I test?+
Empty array with target 0 returns 1, empty array with any other target returns 0. Target 0 on any array returns 1 because the empty subset counts. Test negatives, like the [2,-4,7] example, and large values near 10^9 for overflow.
How do I prepare in 48 hours?+
Write meet in the middle once from scratch. Practice generating subset sums with bitmasks or recursion, then matching with a hash set or sorted list with two pointers. Run both examples and a 30-element random test to check speed.