Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
Example 2:
Input: nums = [0] Output: [[],[0]]
Constraints:
1 <= nums.length <= 10-10 <= nums[i] <= 10Problem summary: Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Backtracking · Bit Manipulation
[1,2,2]
[0]
subsets)find-array-given-subset-sums)import java.util.*;
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
backtrack(0, nums, new ArrayList<>(), ans);
return ans;
}
private void backtrack(int start, int[] nums, List<Integer> cur, List<List<Integer>> ans) {
ans.add(new ArrayList<>(cur));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) continue;
cur.add(nums[i]);
backtrack(i + 1, nums, cur, ans);
cur.remove(cur.size() - 1);
}
}
}
import "sort"
func subsetsWithDup(nums []int) [][]int {
sort.Ints(nums)
ans := [][]int{}
cur := []int{}
var backtrack func(start int)
backtrack = func(start int) {
ans = append(ans, append([]int{}, cur...))
for i := start; i < len(nums); i++ {
if i > start && nums[i] == nums[i-1] {
continue
}
cur = append(cur, nums[i])
backtrack(i + 1)
cur = cur[:len(cur)-1]
}
}
backtrack(0)
return ans
}
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
nums.sort()
ans: List[List[int]] = []
cur: List[int] = []
def backtrack(start: int) -> None:
ans.append(cur[:])
for i in range(start, len(nums)):
if i > start and nums[i] == nums[i - 1]:
continue
cur.append(nums[i])
backtrack(i + 1)
cur.pop()
backtrack(0)
return ans
impl Solution {
pub fn subsets_with_dup(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
nums.sort();
let mut ans = Vec::new();
let mut cur = Vec::new();
fn backtrack(start: usize, nums: &Vec<i32>, cur: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>) {
ans.push(cur.clone());
for i in start..nums.len() {
if i > start && nums[i] == nums[i - 1] {
continue;
}
cur.push(nums[i]);
backtrack(i + 1, nums, cur, ans);
cur.pop();
}
}
backtrack(0, &nums, &mut cur, &mut ans);
ans
}
}
function subsetsWithDup(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const ans: number[][] = [];
const cur: number[] = [];
const backtrack = (start: number): void => {
ans.push([...cur]);
for (let i = start; i < nums.length; i++) {
if (i > start && nums[i] === nums[i - 1]) continue;
cur.push(nums[i]);
backtrack(i + 1);
cur.pop();
}
};
backtrack(0);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.