Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the current subset to the result list.
DSA C#
result.Add(new List<int>([1]));Attempts:
3 left
💡 Hint
Common Mistakes
Adding the original list 'nums' instead of the current subset.
Adding the result list to itself.
Using an undefined variable like 'subset'.
✗ Incorrect
We add a copy of the current subset to the result list to store one subset of the powerset.
2fill in blank
mediumComplete the code to include the next element in the current subset.
DSA C#
currentSubset.[1](nums[index]);Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove which deletes an element.
Using Clear which empties the list.
Using Insert without specifying the position.
✗ Incorrect
We use Add to include the element at nums[index] into the current subset.
3fill in blank
hardFix the error in the recursive call to generate subsets.
DSA C#
GenerateSubsets(nums, index [1], currentSubset, result);Attempts:
3 left
💡 Hint
Common Mistakes
Using -1 which moves backward and causes infinite recursion.
Using *1 or /1 which does not change the index.
✗ Incorrect
We increase the index by 1 to move to the next element in the array.
4fill in blank
hardFill both blanks to remove the last element and backtrack after recursion.
DSA C#
currentSubset.[1](currentSubset.[2] - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove which removes by value, not index.
Using Add instead of RemoveAt.
Using Count as a method call instead of a property.
✗ Incorrect
RemoveAt removes the element at the specified index, and Count gives the number of elements. We remove the last element added to backtrack.
5fill in blank
hardFill all three blanks to complete the recursive powerset generation method.
DSA C#
public void GenerateSubsets(int[] nums, int [1], List<int> [2], List<List<int>> [3]) { if ([1] == nums.Length) { [3].Add(new List<int>([2])); return; } // Include nums[index] [2].Add(nums[[1]]); GenerateSubsets(nums, [1] + 1, [2], [3]); [2].RemoveAt([2].Count - 1); // Exclude nums[index] GenerateSubsets(nums, [1] + 1, [2], [3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names that do not match the method signature.
Confusing the array nums with the current subset list.
✗ Incorrect
The method uses 'index' to track position, 'currentSubset' to hold the current subset, and 'result' to store all subsets.
