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 of unique elements, 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,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0] Output: [[],[0]]
Constraints:
1 <= nums.length <= 10-10 <= nums[i] <= 10nums are unique.Problem summary: Given an integer array nums of unique elements, 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,3]
[0]
subsets-ii)generalized-abbreviation)letter-case-permutation)find-array-given-subset-sums)count-number-of-maximum-bitwise-or-subsets)import java.util.*;
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
for (int x : nums) {
int size = ans.size();
for (int i = 0; i < size; i++) {
List<Integer> next = new ArrayList<>(ans.get(i));
next.add(x);
ans.add(next);
}
}
return ans;
}
}
func subsets(nums []int) [][]int {
ans := [][]int{{}}
for _, x := range nums {
size := len(ans)
for i := 0; i < size; i++ {
next := append([]int{}, ans[i]...)
next = append(next, x)
ans = append(ans, next)
}
}
return ans
}
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
ans = [[]]
for x in nums:
ans += [subset + [x] for subset in ans]
return ans
impl Solution {
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut ans: Vec<Vec<i32>> = vec![vec![]];
for x in nums {
let size = ans.len();
for i in 0..size {
let mut next = ans[i].clone();
next.push(x);
ans.push(next);
}
}
ans
}
}
function subsets(nums: number[]): number[][] {
const ans: number[][] = [[]];
for (const x of nums) {
const size = ans.length;
for (let i = 0; i < size; i++) {
ans.push([...ans[i], x]);
}
}
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.