Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the digit at a specific place value.
DSA C#
int GetDigit(int number, int place) {
return (number / [1]) % 10;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'place' directly instead of 10^place.
Dividing by 10 instead of 10^place.
✗ Incorrect
We divide the number by 10 raised to the power of place to shift the digit to the units place, then take modulo 10 to get that digit.
2fill in blank
mediumComplete the code to find the maximum number in the array.
DSA C#
int GetMax(int[] arr) {
int max = arr[0];
foreach (int num in arr) {
if (num [1] max) {
max = num;
}
}
return max;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which finds minimum instead of maximum.
Using '==' which does not update max.
✗ Incorrect
We update max when we find a number greater than the current max.
3fill in blank
hardFix the error in the counting sort step for radix sort.
DSA C#
void CountingSort(int[] arr, int place) {
int n = arr.Length;
int[] output = new int[n];
int[] count = new int[10];
for (int i = 0; i < n; i++) {
int digit = (arr[i] / [1]) % 10;
count[digit]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / place) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number like 10 instead of 'place'.
Using 1 which always gets the last digit.
✗ Incorrect
We must divide by 'place' to get the correct digit for counting sort.
4fill in blank
hardFill both blanks to complete the main radix sort loop.
DSA C#
void RadixSort(int[] arr) {
int max = GetMax(arr);
for (int place = 1; place [1] max; place = place [2] 10) {
CountingSort(arr, place);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which may miss the highest place value when max is a power of 10.
Using '+' instead of '*' which does not move to next digit place.
✗ Incorrect
We loop while place is less than or equal to max and multiply place by 10 each time.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps digits to counts.
DSA C#
var countDict = new Dictionary<int, int>(); foreach (var num in arr) { int digit = (num / [1]) % 10; if (!countDict.ContainsKey(digit)) { countDict[[2]] = 0; } countDict[digit] += [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num' as key instead of 'digit'.
Incrementing by 'num' instead of 1.
✗ Incorrect
We divide by 'place' to get the digit, use 'digit' as key, and increment count by 1.
