Challenge - 5 Problems
Counting Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Counting Sort on a small array
What is the output of the following counting sort implementation on the array [4, 2, 2, 8, 3, 3, 1]?
DSA Javascript
function countingSort(arr) {
const max = Math.max(...arr);
const count = new Array(max + 1).fill(0);
for (const num of arr) {
count[num]++;
}
let sortedIndex = 0;
for (let i = 0; i < count.length; i++) {
while (count[i] > 0) {
arr[sortedIndex++] = i;
count[i]--;
}
}
return arr;
}
console.log(countingSort([4, 2, 2, 8, 3, 3, 1]));Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then rebuilds the array in order.
✗ Incorrect
The counting sort counts the frequency of each number and then places them back in sorted order. The input array has one 1, two 2s, two 3s, one 4, and one 8. So the sorted array is [1, 2, 2, 3, 3, 4, 8].
🧠 Conceptual
intermediate1:30remaining
Counting Sort Stability Property
Which statement correctly describes the stability of counting sort?
Attempts:
2 left
💡 Hint
Think about whether equal numbers keep their original order after sorting.
✗ Incorrect
Counting sort is stable because it places elements in the output array in the order they appear in the input, preserving the relative order of equal elements.
🔧 Debug
advanced2:00remaining
Identify the error in this counting sort code
What error will this code produce when run on the array [3, 1, 2]?
DSA Javascript
function countingSort(arr) {
const max = Math.max(...arr);
const count = new Array(max).fill(0);
for (const num of arr) {
count[num]++;
}
let sortedIndex = 0;
for (let i = 0; i < count.length; i++) {
while (count[i] > 0) {
arr[sortedIndex++] = i;
count[i]--;
}
}
return arr;
}
console.log(countingSort([3, 1, 2]));Attempts:
2 left
💡 Hint
Check how the count array is created and accessed.
✗ Incorrect
The count array is created with length max (3), so indices 0,1,2 exist. But the code tries to access count[3] which is undefined, causing a TypeError when incrementing count[3].
🚀 Application
advanced2:30remaining
Counting Sort with Negative Numbers
Given the array [-5, -10, 0, -3, 8, 5, -1, 10], which approach correctly adapts counting sort to handle negative numbers?
Attempts:
2 left
💡 Hint
Counting sort uses array indices as counts, so negative indices are invalid.
✗ Incorrect
Counting sort cannot use negative indices. Shifting all numbers by adding the absolute value of the minimum number makes all numbers non-negative, allowing counting sort to work. After sorting, shift back to original values.
❓ Predict Output
expert2:30remaining
Output of Counting Sort with Frequency Array
What is the output of this code snippet?
DSA Javascript
function countingSort(arr) {
const max = Math.max(...arr);
const count = new Array(max + 1).fill(0);
for (const num of arr) {
count[num]++;
}
const result = [];
for (let i = 0; i < count.length; i++) {
for (let j = 0; j < count[i]; j++) {
result.push(i);
}
}
return result;
}
const input = [2, 5, 3, 0, 2, 3, 0, 3];
console.log(countingSort(input));Attempts:
2 left
💡 Hint
Count how many times each number appears and list them in order.
✗ Incorrect
The input has two 0s, two 2s, three 3s, and one 5. So the sorted output is [0, 0, 2, 2, 3, 3, 3, 5].