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 = [1];
*a = *b;
*b = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'a' instead of '*a' which stores the pointer address, not the value.
Using '*b' which would overwrite the wrong value.
✗ Incorrect
The value pointed by 'a' is stored in 'temp' to swap the values correctly.
2fill in blank
mediumComplete the code to perform a single pass of Bubble Sort comparing adjacent elements.
DSA C
void bubblePass(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
if (arr[i] [1] arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which would sort in descending order incorrectly here.
Using '==' or '!=' which do not help in sorting.
✗ Incorrect
In Bubble Sort, swap if the current element is greater than the next to sort in ascending order.
3fill in blank
hardFix the error in the Counting Sort function to correctly count frequencies.
DSA C
void countingSort(int arr[], int n, int maxVal) {
int count[maxVal + 1];
for (int i = 0; i <= maxVal; i++) {
count[i] = 0;
}
for (int i = 0; i < n; i++) {
count[[1]]++;
}
int index = 0;
for (int i = 0; i <= maxVal; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' which is the loop index, not the value to count.
Using 'count[i]' which causes wrong indexing.
✗ Incorrect
We increment count for the value at arr[i], so use arr[i] as index.
4fill in blank
hardComplete the while condition in the Insertion Sort inner loop for ascending order.
DSA C
for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j [1] 0 && arr[j] [2] key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' for the first blank may incorrectly handle j=0.
Using '<' for the second blank shifts elements in the wrong direction for ascending sort.
✗ Incorrect
Use '>=' to check j >= 0 and '>' to check if arr[j] > key so larger elements are shifted right for ascending order.
5fill in blank
hardComplete the Lomuto partition function for Quick Sort (ascending). Assume swap(void *a, void *b) is defined.
DSA C
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j [1] high; j++) {
if (arr[j] <= pivot) {
i++;
[2](&arr[i], &arr[j]);
}
}
[3](&arr[i + 1], &arr[high]);
return i + 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' for the loop condition processes the pivot too early.
Omitting or misspelling the swap function call.
✗ Incorrect
Loop with j < high to scan before pivot, swap elements <= pivot to left, and swap pivot to its final position.
