0
0
DSA Javascriptprogramming~20 mins

Counting Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Counting Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]));
A[1, 2, 3, 4, 8]
B[1, 2, 2, 3, 3, 4, 8]
C[8, 4, 3, 3, 2, 2, 1]
D[1, 1, 2, 2, 3, 3, 4, 8]
Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then rebuilds the array in order.
🧠 Conceptual
intermediate
1:30remaining
Counting Sort Stability Property
Which statement correctly describes the stability of counting sort?
ACounting sort is unstable because it uses a hash map internally.
BCounting sort is unstable because it rearranges equal elements randomly.
CCounting sort is stable only if the input array is already sorted.
DCounting sort is stable because it preserves the relative order of equal elements.
Attempts:
2 left
💡 Hint
Think about whether equal numbers keep their original order after sorting.
🔧 Debug
advanced
2: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]));
ATypeError: Cannot read property '3' of undefined
BRangeError: Maximum call stack size exceeded
COutput: [1, 2, 3]
DReferenceError: max is not defined
Attempts:
2 left
💡 Hint
Check how the count array is created and accessed.
🚀 Application
advanced
2: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?
AConvert all numbers to strings and sort lexicographically.
BIgnore negative numbers and sort only positive numbers using counting sort.
CShift all numbers by adding the absolute value of the minimum number before sorting, then shift back after sorting.
DUse counting sort directly without changes; it handles negatives automatically.
Attempts:
2 left
💡 Hint
Counting sort uses array indices as counts, so negative indices are invalid.
Predict Output
expert
2: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));
A[0, 0, 2, 2, 3, 3, 3, 5]
B[0, 0, 2, 3, 3, 3, 5]
C[0, 0, 2, 3, 3, 5]
D[0, 0, 2, 3, 5]
Attempts:
2 left
💡 Hint
Count how many times each number appears and list them in order.