Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the maximum value from the array for Radix Sort.
DSA Java
int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] [1] max) {
max = arr[i];
}
}
return max;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the max to never update correctly.
Using '==' does not update max properly.
✗ Incorrect
We compare each element with the current max and update max if the element is greater.
2fill in blank
mediumComplete the code to perform counting sort on the array based on the digit represented by exp.
DSA Java
void countSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
for (int i = 0; i < n; i++) {
count[(arr[i] / [1]) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 10 instead of exp ignores the current digit place.
Using arr[i] directly does not isolate the digit.
✗ Incorrect
We divide each element by exp to isolate the digit at the current place value.
3fill in blank
hardFix the error in the loop that performs counting sort for Radix Sort.
DSA Java
for (int i = n - 1; i >= 0; i--) { output[count[(arr[i] / [1]) % 10] - 1] = arr[i]; count[(arr[i] / [1]) % 10]--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' or 'n' instead of 'exp' causes wrong digit extraction.
Using 10 ignores the current digit place.
✗ Incorrect
The division should be by exp to correctly find the digit at the current place value.
4fill in blank
hardFill both blanks to complete the main Radix Sort loop that sorts by each digit place.
DSA Java
void radixSort(int[] arr) {
int max = getMax(arr);
for (int exp = 1; max [1] exp; exp = exp [2] 10) {
countSort(arr, exp);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes the loop to never run.
Using '+' instead of '*' does not move to the next digit place correctly.
✗ Incorrect
The loop runs while exp is less than or equal to max, multiplying exp by 10 each time.
5fill in blank
hardFill all three blanks to complete the counting sort initialization and update steps.
DSA Java
void countSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
for (int i = 0; i < n; i++) {
count[(arr[i] / [1]) % 10]++;
}
for (int i = 1; i [2] 10; i++) {
count[i] = count[i] [3] count[i - 1];
}
// rest of the code
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators in the loop condition or accumulation causes incorrect counting.
Using '-' instead of '+' breaks the cumulative count.
✗ Incorrect
We divide by exp to get the digit, loop while i < 10, and accumulate counts by adding previous counts.
