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[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[1];
arr[j] = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for array indexing.
Swapping with wrong indices.
✗ Incorrect
To swap elements at positions i and j, we assign arr[i] = arr[j]. The correct syntax uses square brackets for indexing.
2fill in blank
mediumComplete the code to choose the pivot element as the last element of the array segment.
DSA C#
int Partition(int[] arr, int low, int high) {
int pivot = arr[1];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
Swap(arr, i, j);
}
}
Swap(arr, i + 1, high);
return i + 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for array indexing.
Choosing the wrong pivot index.
✗ Incorrect
The pivot is chosen as the last element in the current segment, which is arr[high].
3fill in blank
hardFix the error in the recursive QuickSort call to sort the left partition.
DSA C#
void QuickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = Partition(arr, low, high);
QuickSort(arr, low, [1]);
QuickSort(arr, pi + 1, high);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pi instead of pi - 1 causes infinite recursion.
Using wrong indices for recursive calls.
✗ Incorrect
To sort the left partition, QuickSort should be called with low and pi - 1 as boundaries.
4fill in blank
hardFill both blanks to complete the base case and the recursive call for the right partition.
DSA C#
void QuickSort(int[] arr, int low, int high) {
if ([1] < [2]) {
int pi = Partition(arr, low, high);
QuickSort(arr, low, pi - 1);
QuickSort(arr, pi + 1, high);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pivot indices in the base case condition.
Incorrect comparison operators.
✗ Incorrect
The base case checks if low is less than high to continue sorting.
5fill in blank
hardFill the blanks to create a dictionary that maps each element to its square if the element is greater than 2.
DSA C#
var result = new Dictionary<int, int>(); foreach (var num in arr) { if (num [1] 2) { result[num] = num [2] num; } } return result;
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Using + instead of * for squaring.
✗ Incorrect
We check if num > 2, then assign result[num] = num * num.
