PPractick

DSA interview question

Target Sum

Hard · Algorithms · Topics: dynamic programming, knapsack

The problem

You're building a "portfolio scenario counter" for a trading-simulation tool. Each entry in an array nums represents a trade's magnitude, but every trade could have gone either long (+) or short (-) — the historical direction wasn't logged, only the magnitudes. Given the array nums and a known final net target, your job is to count how many distinct sign assignments (one + or - per element) result in an expression that sums exactly to target. This tells the risk team how many plausible historical scenarios could explain the observed final outcome.

Function signature

int findTargetSumWays(vector<int>& nums, int target);

Test cases

numstargetOutputExplanation
[1,1,1,1,1]355 distinct sign assignments sum to 3 (e.g. +1+1+1+1-1, and 4 other permutations of one minus)
[1]11only +1 works
[1]20impossible — max achievable magnitude is 1
[0,0,0,0,0,0,0,0,1]1256each 0 can independently be + or - (no effect), giving 2^8 = 256 combinations, all valid
[]01empty array, target 0 → one way: the empty assignment
[100]-1001only -100 works
[1,2,3]02+1+2-3 and -1-2+3

(Watch the [0,0,...,0,1] case — it's a classic trap testing whether your subset-sum reduction correctly handles zero-valued elements, which double the count of every valid subset they're added to.)

How interviewers phrase it

Clarifying questions to ask

  1. Can array elements be 0? (Critical edge case — zeros don't affect the sum but still double the combinatorial count, since + and - both give 0.)
  2. Are elements guaranteed non-negative? (Standard problem statement assumes nums[i] >= 0; the sign is what you're choosing, not an inherent property of the input.)
  3. Can target be negative?
  4. What's the expected size of nums and magnitude of values? (Signals whether brute-force 2^n recursion needs to become DP, and what capacity range the DP subset-sum table needs.)
  5. Do I need to enumerate the actual sign assignments, or just count them?

Follow-ups to expect

  1. "Can you reduce this to Subset Sum / 0-1 Knapsack?"
  2. "What's the brute-force complexity, and why is it bad?"
  3. "Can you optimize space?"
  4. "What if elements could be negative in the input?"
  5. "How does this compare to Partition Equal Subset Sum?"
  6. "Can you solve it with pure recursion + memoization instead of converting to subset sum?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in