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(int arr[], int n) {
int subset[[1]];
generateSubsetsHelper(arr, n, 0, subset, 0);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as size causes no space for elements.
Using n+1 wastes extra space unnecessarily.
✗ Incorrect
The subset array should have the same size as the input array to hold any subset.
2fill in blank
mediumComplete the base case condition to print the current subset when all elements are processed.
DSA C
void generateSubsetsHelper(int arr[], int n, int index, int subset[], int subsetSize) {
if (index [1] n) {
for (int i = 0; i < subsetSize; i++) {
printf("%d ", subset[i]);
}
printf("\n");
return;
}
// Recursive calls...
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or <= causes premature printing.
Using > never triggers base case.
✗ Incorrect
When index equals n, all elements have been considered, so print the subset.
3fill in blank
hardFix the error in the recursive call that includes the current element in the subset.
DSA C
subset[subsetSize] = arr[index]; generateSubsetsHelper(arr, n, [1], subset, subsetSize + 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not incrementing index causes infinite recursion.
Using subsetSize instead of index is incorrect.
✗ Incorrect
We move to the next index after including the current element.
4fill in blank
hardFill both blanks to correctly make the recursive call that excludes the current element.
DSA C
generateSubsetsHelper(arr, n, [1], subset, [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not incrementing index causes infinite recursion.
Increasing subsetSize when excluding element is wrong.
✗ Incorrect
We skip the current element by moving index forward but keep subsetSize unchanged.
5fill in blank
hardFill all three blanks to complete the recursive function that generates all subsets.
DSA C
void generateSubsetsHelper(int arr[], int n, int index, int subset[], int subsetSize) {
if (index [1] n) {
for (int i = 0; i < subsetSize; i++) {
printf("%d ", subset[i]);
}
printf("\n");
return;
}
subset[subsetSize] = arr[index];
generateSubsetsHelper(arr, n, [2], subset, subsetSize + 1);
generateSubsetsHelper(arr, n, [3], subset, subsetSize);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong base case condition.
Not incrementing index in recursive calls.
✗ Incorrect
Base case when index == n; recursive calls increment index to include or exclude element.