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 = [1];
*b = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning temp to *a instead of *b.
Using variable names without dereferencing.
✗ Incorrect
The value pointed by b (*b) is assigned to *a to complete the swap.
2fill in blank
mediumComplete the for loop condition to reverse the array by swapping elements.
DSA C
for (int i = 0; i < [1] / 2; i++) { swap(&arr[i], &arr[[1] - i - 1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names for array size.
Looping till the full array length instead of half.
✗ Incorrect
The variable n represents the size of the array and is used to find the midpoint.
3fill in blank
hardFix the error in the while loop condition to reverse the array.
DSA C
int start = 0, end = n - 1; while (start [1] end) { swap(&arr[start], &arr[end]); start++; end--; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes swapping the middle element twice.
Using > or >= causes the loop to never run.
✗ Incorrect
The loop should run while start is less than end to avoid swapping the middle element twice.
4fill in blank
hardFill both blanks to create a for loop that reverses the array using two indices.
DSA C
for (int i = 0, j = [1] - 1; i < j; i++, [2]--) { swap(&arr[i], &arr[j]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names for array size.
Incrementing j instead of decrementing.
✗ Incorrect
The loop starts with j at n-1 and decrements j each iteration.
5fill in blank
hardFill all three blanks to create a function that reverses an array in place.
DSA C
void reverseArray(int arr[], int [1]) { int start = 0, end = [2] - 1; while (start [3] end) { swap(&arr[start], &arr[end]); start++; end--; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Using <= instead of < in the loop condition.
✗ Incorrect
The function uses n as the parameter name, n for the array length, and < in the while loop condition.
