Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the recursive function call for generating all subsets.
DSA C++
void generateSubsets(vector<int>& nums, vector<int>& current, int index, vector<vector<int>>& result) {
if (index == nums.size()) {
result.push_back(current);
return;
}
// Include nums[index]
current.push_back(nums[index]);
generateSubsets(nums, current, [1], result);
current.pop_back();
// Exclude nums[index]
generateSubsets(nums, current, index + 1, result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index - 1 causes infinite recursion or wrong subsets.
Using 0 resets the index and causes infinite loops.
✗ Incorrect
We move to the next index by adding 1 to the current index to explore subsets including the current element.
2fill in blank
mediumComplete the code to initialize the subsets generation and return the result.
DSA C++
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result;
vector<int> current;
generateSubsets(nums, current, [1], result);
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 skips the first element.
Starting from nums.size() means starting beyond the last element.
✗ Incorrect
We start from index 0 to include or exclude elements from the beginning.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA C++
void generateSubsets(vector<int>& nums, vector<int>& current, int index, vector<vector<int>>& result) {
if (index [1] nums.size()) {
result.push_back(current);
return;
}
current.push_back(nums[index]);
generateSubsets(nums, current, index + 1, result);
current.pop_back();
generateSubsets(nums, current, index + 1, result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes recursion to stop too early.
Using <= or > causes out-of-bound errors.
✗ Incorrect
The recursion stops when index equals the size of nums, meaning all elements are processed.
4fill in blank
hardFill both blanks to create a loop that generates subsets iteratively.
DSA C++
vector<vector<int>> subsetsIterative(vector<int>& nums) {
vector<vector<int>> result = [1];
for (int num : nums) {
int n = result.size();
for (int i = 0; i < n; i++) {
vector<int> subset = result[i];
subset.push_back(num);
result.[2](subset);
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of push_back causes wrong insertion.
Starting with an empty vector instead of a vector containing an empty vector misses the empty subset.
✗ Incorrect
Start with an empty subset represented by an empty vector inside result, and add new subsets using push_back.
5fill in blank
hardFill all three blanks to create a recursive function that generates subsets and uses backtracking.
DSA C++
void backtrack(vector<int>& nums, int [1], vector<int>& tempList, vector<vector<int>>& result) { result.push_back(tempList); for (int i = [2]; i < nums.size(); i++) { tempList.push_back(nums[i]); backtrack(nums, i + [3], tempList, result); tempList.pop_back(); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 in the loop start causes repeated subsets.
Using start instead of index causes undefined variable errors.
Using i + 0 in recursion causes infinite loops.
✗ Incorrect
Use 'index' as the starting point, loop from index, and recurse with i + 1 to move forward.
