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 counting sort implementation on the array [4, 2, 2, 8, 3, 3, 1]?
DSA Python
def counting_sort(arr): max_val = max(arr) count = [0] * (max_val + 1) for num in arr: count[num] += 1 sorted_arr = [] for i, c in enumerate(count): sorted_arr.extend([i] * c) return sorted_arr print(counting_sort([4, 2, 2, 8, 3, 3, 1]))
Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then reconstructs the sorted list.
✗ Incorrect
The counting sort counts the frequency of each number and then outputs them in order. The input array has one 1, two 2s, two 3s, one 4, and one 8, so the sorted output is [1, 2, 2, 3, 3, 4, 8].
🧠 Conceptual
intermediateCounting Sort Characteristics
Which of the following statements about counting sort is TRUE?
Attempts:
2 left
💡 Hint
Think about how counting sort uses an array indexed by input values.
✗ Incorrect
Counting sort uses an array sized by the range of input values. If the range is much larger than the number of elements, it becomes inefficient. It is not comparison-based and does not handle negative numbers without modification.
🔧 Debug
advancedIdentify the Bug in Counting Sort Implementation
What error will this counting sort code produce when run on the array [3, 1, 2]?
DSA Python
def counting_sort(arr): max_val = max(arr) count = [0] * max_val for num in arr: count[num] += 1 sorted_arr = [] for i, c in enumerate(count): sorted_arr.extend([i] * c) return sorted_arr print(counting_sort([3, 1, 2]))
Attempts:
2 left
💡 Hint
Check how the count array size relates to max_val and indexing.
✗ Incorrect
The count array is created with size max_val, but since list indices start at 0, the index max_val is out of range. For example, max_val=3 but count has length 3 (indices 0,1,2). Accessing count[3] causes IndexError.
❓ Predict Output
advancedCounting Sort Output with Duplicate and Zero
What is the output of this counting sort code on the array [0, 5, 3, 0, 2, 5, 1]?
DSA Python
def counting_sort(arr): max_val = max(arr) count = [0] * (max_val + 1) for num in arr: count[num] += 1 sorted_arr = [] for i, c in enumerate(count): sorted_arr.extend([i] * c) return sorted_arr print(counting_sort([0, 5, 3, 0, 2, 5, 1]))
Attempts:
2 left
💡 Hint
Remember counting sort includes all duplicates and zeros in the output.
✗ Incorrect
The input has two zeros, one 1, one 2, one 3, and two 5s. Counting sort outputs all in sorted order including duplicates.
🧠 Conceptual
expertCounting Sort Stability and Use Cases
Which statement about counting sort is CORRECT?
Attempts:
2 left
💡 Hint
Think about whether counting sort preserves the order of equal elements and what types of keys it handles.
✗ Incorrect
Counting sort is stable, meaning it preserves the order of equal elements. It works well for sorting objects by integer keys in a small range without comparisons.
