Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two integers using a temporary variable.
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 'a' instead of '*b'.
Using 'temp' instead of '*b'.
✗ Incorrect
We assign the value pointed by b to the location pointed by a to complete the swap.
2fill in blank
mediumComplete the code to move the 'low' pointer forward when the current element is less than pivot.
DSA C
if (arr[i] < pivot) { swap(&arr[i], &arr[[1]]); low++; i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'high' instead of 'low'.
Swapping with 'i' which is the current index.
✗ Incorrect
We swap with the element at 'low' pointer and then increment 'low'.
3fill in blank
hardFix the error in the loop condition to correctly process all elements.
DSA C
while ([1] <= high) { if (arr[i] < pivot) { swap(&arr[i], &arr[low]); low++; i++; } else if (arr[i] > pivot) { swap(&arr[i], &arr[high]); high--; } else { i++; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'low' or 'high' as the loop variable incorrectly.
Comparing 'pivot' instead of an index.
✗ Incorrect
The index 'i' should be compared to 'high' to process all elements.
4fill in blank
hardFill both blanks to initialize pointers correctly before the partition loop.
DSA C
int low = [1]; int high = [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'low' to 1 instead of 0.
Setting 'high' to size instead of size - 1.
✗ Incorrect
The 'low' pointer starts at 0 and 'high' at the last index (size - 1).
5fill in blank
hardFill all three blanks to complete the Dutch National Flag partition function.
DSA C
void dutch_national_flag(int arr[], int size, int pivot) {
int low = [1];
int high = [2];
int i = [3];
while (i <= high) {
if (arr[i] < pivot) {
swap(&arr[i], &arr[low]);
low++;
i++;
} else if (arr[i] > pivot) {
swap(&arr[i], &arr[high]);
high--;
} else {
i++;
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting 'i' or 'low' at 1 instead of 0.
Setting 'high' to size instead of size - 1.
✗ Incorrect
Initialize 'low' and 'i' to 0, and 'high' to size - 1 to cover the whole array.
