Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the current subset to the result list.
DSA Java
result.add(new ArrayList<>(subset[1]));Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Trying to add the subset directly without copying.
✗ Incorrect
We use parentheses to call the ArrayList constructor to create a copy of the current subset.
2fill in blank
mediumComplete the code to add the current element nums[index] to the subset.
DSA Java
subset.[1](nums[index]);Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is not a method in ArrayList.
Using push which is from other languages.
✗ Incorrect
In Java's ArrayList, the method to add an element is add().
3fill in blank
hardFix the error in the recursive call to generate subsets.
DSA Java
generateSubsets(nums, [1] + 1, subset, result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using i which is undefined in this scope.
Using subset.size() or result.size() which are unrelated.
✗ Incorrect
The recursive call should move to the next index, so we add 1 to the current index variable.
4fill in blank
hardFill both blanks to remove the last element from subset after recursion.
DSA Java
subset.[1](subset.[2]() - 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which is not a method in ArrayList.
Using length() which is not a method in ArrayList.
✗ Incorrect
We remove the element at the last index which is size() - 1 in ArrayList.
5fill in blank
hardFill all three blanks to complete the method signature and base case check.
DSA Java
public void generateSubsets(int[] [1], int [2], List<Integer> subset, List<List<Integer>> [3]) { if ([2] == [1].length) { [3].add(new ArrayList<>(subset)); return; } // rest of code }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing parameter names or using undefined variables.
Not matching parameter names with those used inside the method.
✗ Incorrect
The method takes nums array, index integer, subset list, and result list as parameters. The base case checks if index equals nums.length and adds subset to result.
