Challenge - 5 Problems
Counting Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Counting Sort on a small array
What is the output of the following C# code that uses counting sort on the array [4, 2, 2, 8, 3, 3, 1]?
DSA C#
using System; class Program { static void CountingSort(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i] > max) max = arr[i]; } int[] count = new int[max + 1]; for (int i = 0; i < arr.Length; i++) { count[arr[i]]++; } int index = 0; for (int i = 0; i < count.Length; i++) { while (count[i] > 0) { arr[index++] = i; count[i]--; } } } static void Main() { int[] arr = {4, 2, 2, 8, 3, 3, 1}; CountingSort(arr); foreach (int num in arr) { Console.Write(num + " "); } } }
Attempts:
2 left
💡 Hint
Counting sort arranges numbers from smallest to largest by counting occurrences.
✗ Incorrect
The counting sort counts how many times each number appears, then writes them back in order. The array is sorted ascending: 1 2 2 3 3 4 8.
🧠 Conceptual
intermediateCounting Sort Stability Property
Which statement best describes the stability property of counting sort?
Attempts:
2 left
💡 Hint
Stability means equal elements keep their original order after sorting.
✗ Incorrect
Counting sort is stable because it places elements in output array in the order they appear in input when counts are processed.
🔧 Debug
advancedIdentify the error in this counting sort implementation
What error will this C# counting sort code produce when run on array [3, 1, 2]?
DSA C#
void CountingSort(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i] > max) max = arr[i]; } int[] count = new int[max + 1]; for (int i = 0; i < arr.Length; i++) { count[arr[i]]++; } int index = 0; for (int i = 0; i < count.Length; i++) { while (count[i] > 0) { arr[index++] = i; count[i]--; } } }
Attempts:
2 left
💡 Hint
Check the size of the count array and the maximum value used as index.
✗ Incorrect
The count array size is max, but max is the largest value in arr. Since arrays are zero-indexed, count[max] is out of range causing IndexOutOfRangeException.
❓ Predict Output
advancedCounting Sort Output with Negative Numbers
What happens when you run counting sort on array [-1, 2, 0, -1] with the standard counting sort code that assumes only non-negative integers?
DSA C#
int[] arr = {-1, 2, 0, -1}; CountingSort(arr); // standard counting sort code without handling negatives
Attempts:
2 left
💡 Hint
Counting sort uses array indices for counts, negative indices are invalid.
✗ Incorrect
Negative numbers cause indexing errors because array indices cannot be negative, so it throws IndexOutOfRangeException.
🚀 Application
expertCounting Sort Time Complexity Analysis
Given an input array of size n with maximum value k, what is the time complexity of counting sort?
Attempts:
2 left
💡 Hint
Counting sort counts occurrences and then writes output based on counts.
✗ Incorrect
Counting sort runs in O(n + k) time because it counts each element once and iterates over the count array of size k.
