DSA interview question
You're building an "audit reconciliation" tool for a finance team. Given an array of transaction amounts, the array must be split into two groups — say S1 and S2 — such that every transaction belongs to exactly one group. The team wants to know: for a target discrepancy D, in how many distinct ways can the transactions be partitioned into two groups such that sum(S1) - sum(S2) = D? This surfaces every plausible way the ledger could have been split to produce that specific discrepancy.
int countPartitions(vector<int>& arr, int D);
arr | D | Output | Explanation |
|---|---|---|---|
[1,1,2,3] | 1 | 3 | 3 distinct partitions into (S1,S2) give a difference of 1 |
[1,1,1,1] | 0 | 3 | 3 ways to split 2-and-2 such that both sides sum to 2 |
[1,2,3,1] | 1 | 3 | same multiset as first test, reordered — count should match structurally |
[1] | 1 | 1 | S1={1}, S2={} → difference 1 |
[] | 0 | 1 | empty array, trivially one partition: both groups empty |
[5] | 10 | 0 | impossible — max achievable difference is 5 |
[0,0,1] | 1 | 4 | zeros can go to either side freely, doubling valid combinations — classic trap case |
(This is the "unsigned" sibling of Target Sum — instead of assigning +/- to a fixed array and hitting an exact target, you're partitioning into two groups with a difference constraint. The math reduction is nearly identical.)
D guaranteed non-negative? (Since sum(S1) - sum(S2) = D could be asked with either group being the "larger" one — usually D is given as non-negative, and by convention S1 is the larger-or-equal side.)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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in