Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA C
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning *a to *b instead of using temp.
Using incorrect pointer dereferencing.
✗ Incorrect
The swap function uses a temporary variable to hold the value of *a before assigning *b to *a, then assigns temp to *b to complete the swap.
2fill in blank
mediumComplete the code to print the current permutation of the array.
DSA C
void printArray(int arr[], int size) {
for (int i = 0; i < [1]; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array name instead of size in the loop condition.
Looping beyond the array bounds.
✗ Incorrect
The loop should run from 0 to size-1 to print all elements of the array.
3fill in blank
hardFix the error in the recursive call to generate permutations.
DSA C
void permute(int arr[], int l, int r) {
if (l == r) {
printArray(arr, r + 1);
} else {
for (int i = l; i <= r; i++) {
swap(&arr[l], &arr[i]);
permute(arr, [1], r);
swap(&arr[l], &arr[i]);
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index in the recursive call.
Not incrementing the left index causing infinite recursion.
✗ Incorrect
The recursive call should move the left index forward by one to generate permutations of the remaining elements.
4fill in blank
hardFill both blanks to complete the for loop and swap calls correctly.
DSA C
for (int i = [1]; i <= [2]; i++) { swap(&arr[l], &arr[i]); permute(arr, l + 1, r); swap(&arr[l], &arr[i]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of l.
Looping beyond r causing out-of-bounds errors.
✗ Incorrect
The loop variable i starts at l and goes up to r to swap elements for generating permutations.
5fill in blank
hardFill all three blanks to complete the main function setup and call permutation.
DSA C
int main() {
int arr[] = [1];
int n = sizeof(arr) / sizeof(arr[0]);
permute(arr, [2], [3]);
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect array initialization.
Passing wrong indices to permute function.
✗ Incorrect
The array is initialized with {1, 2, 3}, and permute is called with indices 0 and n-1 to cover the full array.