DSA interview question
You're building a quick pre-check for the "fair team split" tool from the previous ticket — before running the full minimum-difference optimization, the product team wants a fast yes/no sanity check: can the given array of skill scores even be split into two teams with exactly equal total skill? Given an array arr, determine whether it can be partitioned into two subsets S1 and S2 such that sum(S1) == sum(S2).
bool canPartition(vector<int>& arr);
arr | Output | Explanation |
|---|---|---|
[1,5,11,5] | true | {1,5,5} sum=11 vs {11} sum=11 — equal split exists |
[1,2,3,5] | false | totalSum=11 is odd — equal split is impossible immediately |
[1,2,3,4,5,6,7] | true | totalSum=28, need subset summing to 14 — e.g. {7,6,1} or {7,4,3} |
[2,2,3,5] | false | totalSum=12, need subset summing to 6 — no combination hits exactly 6 |
[1] | false | single element, can't split into two non-trivial... actually check: totalSum=1 is odd → false |
[] | true | empty array — both sides trivially empty and equal (sum 0 each) |
[0,0] | true | totalSum=0 (even), subset {} or {0,0} both sum to 0 — trivially balanced |
(First and cheapest check to always mention: if totalSum is odd, the answer is immediately false — no need to run any DP at all.)
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