Partition Equal Subset Sum — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Given an array of positive integers, can you partition it into two subsets with equal sums?” If you recognize the reduction — “can any subset sum to total / 2?” — you’re halfway there. This is a 0/1 knapsack DP problem, and it rewards candidates who can identify the classic reduction, define the DP state cleanly, and implement the space-optimized 1D version.
The Problem
Given an integer array nums, return true if you can partition the array into two subsets such that the sum of elements in both subsets is equal, and false otherwise.
Example 1
Input
nums = [1, 5, 11, 5]
Output
true — partition into [1, 5, 5] and [11]
Example 2
Input
nums = [1, 2, 3, 5]
Output
false — no valid partition
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Problem reduction | Do you reduce “equal partition” to “subset sum = total/2”? |
| 0/1 knapsack DP | Do you recognize and apply the classic pattern? |
| Space optimization | Can you reduce from 2D DP to 1D by iterating in reverse? |
| Early termination | Do you check for odd total and return false immediately? |
| State definition | Can you define dp[j] precisely: “can we form sum j from elements seen so far”? |
Step 1: Clarifying Questions
1. Can elements be negative?
No — the problem constrains nums[i] >= 1.
2. What if the total sum is odd?
Return false immediately. Two equal-sum partitions require an even total.
3. What if the array has one element?
A single element can’t be split into two non-empty subsets — return false.
4. Are elements bounded?
1 <= nums[i] <= 100 and 1 <= len(nums) <= 200. Target sum is at most 200 * 100 / 2 = 10000. The DP table is manageable.
Step 2: The Key Reduction
If the total sum is S:
- For a valid partition, both halves must sum to
S / 2. - So the question reduces to: can any subset of
numssum toS / 2?
This is the 0/1 knapsack subset sum problem.
Step 3: The Key Insight
Define
dp[j]asTrueif some subset of elements seen so far sums to exactlyj. For each new elementnum, updating in reverse order (fromtargetdown tonum) ensures each element is used at most once. Ifdp[target]becomesTrue, the partition exists.
---
Step 4: Algorithm
- Compute
total = sum(nums). Iftotal % 2 != 0, returnFalse. - Set
target = total // 2. - Initialize
dp = [False] * (target + 1). Setdp[0] = True(empty subset sums to 0). - For each
numinnums:- Iterate
jfromtargetdown tonum:dp[j] = dp[j] or dp[j - num]
- Iterate
- Return
dp[target].
Python Implementation
2D DP (easier to derive, then optimize):
def canPartition_2d(nums: list[int]) -> bool:
total = sum(nums)
if total % 2 != 0:
return False
target = total // 2
n = len(nums)
# dp[i][j] = True if first i elements can sum to j
dp = [[False] * (target + 1) for _ in range(n + 1)]
for i in range(n + 1):
dp[i][0] = True # empty subset always sums to 0
for i in range(1, n + 1):
num = nums[i - 1]
for j in range(target + 1):
dp[i][j] = dp[i - 1][j] # don't include nums[i-1]
if j >= num:
dp[i][j] = dp[i][j] or dp[i - 1][j - num] # include it
return dp[n][target]
1D DP (space-optimized — what you should write in an interview):
def canPartition(nums: list[int]) -> bool:
total = sum(nums)
if total % 2 != 0:
return False
target = total // 2
dp = [False] * (target + 1)
dp[0] = True
for num in nums:
for j in range(target, num - 1, -1): # iterate in reverse
dp[j] = dp[j] or dp[j - num]
if dp[target]: # early exit
return True
return dp[target]
Time & Space Complexity
| Time | Space | |
|---|---|---|
| 2D DP | O(n * target) | O(n * target) |
| 1D DP | O(n * target) | O(target) |
target is at most sum(nums) / 2. With constraints, this is at most 10,000.
Common Interview Mistakes
-
Iterating forward in the 1D DP. If you iterate
jfromnumtotarget(increasing), you can usenummultiple times in the same pass — that models the unbounded knapsack, not 0/1 knapsack. Reverse iteration enforces each element is used at most once. -
Forgetting
dp[0] = True. The base case is that the empty subset has sum 0. Without this, no subset can ever be “built up” in the DP. -
Not handling odd totals early. Always check
total % 2 != 0before building the DP table. Saves unnecessary work and is a clear correctness check interviewers notice. -
Confusing subset sum with partition. The partition constraint means you don’t need to find which elements are in each half — just whether one subset summing to
targetexists. The other elements automatically form the complement.
What a Strong Candidate Sounds Like
“The key reduction: for a valid partition, both subsets sum to
total / 2. So I just need to check if any subset sums totarget = total / 2. This is 0/1 knapsack. I’ll use a 1D DP:dp[j]is true if some subset sums toj. For each element, I iteratejfromtargetdown tonum— reverse order ensures each element is used at most once. Time is O(n * target), space is O(target).”
Example Interview Dialogue
Interviewer: Why iterate in reverse in the 1D DP?
Candidate: In the 0/1 knapsack, each element can be used at most once. In the 1D DP, dp[j] depends on dp[j - num] from the same row. If I iterate forward, by the time I compute dp[j], dp[j - num] has already been updated with the current element — so I might add the element again. Iterating backward ensures dp[j - num] still reflects the state before the current element was considered.
Interviewer: What’s the worst-case input size?
Candidate: len(nums) = 200, nums[i] = 100, so target is at most 10,000. The DP table is 200 * 10,000 = 2 million operations — fast and well within time limits.
Related Problems to Practice
- Target Sum (LeetCode #494). Assign +/- to each element to reach a target — reduces to subset sum.
- Last Stone Weight II (LeetCode #1049). Minimize the difference between two subset sums — same knapsack reduction.
- Coin Change (LeetCode #322). Unbounded knapsack — compare with 0/1 knapsack to understand the forward/reverse iteration difference.
- Subsets (LeetCode #78). Enumerate all subsets — complement to subset-sum DP.
Practice This in a Mock Interview
Partition Equal Subset Sum is the canonical 0/1 knapsack interview problem. The reduction to subset sum and the reverse-iteration trick in the 1D DP are the two things you need to lock in. If you can derive the 2D DP first and then optimize to 1D while explaining why the iteration direction changes, that’s an extremely strong answer.
Start a mock interview for Partition Equal Subset Sum on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap and study plans
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub, condensed solutions with code