Challenge - 5 Problems
Counting Sort Mastery
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 Go code that implements counting sort on the array [4, 2, 2, 8, 3, 3, 1]?
DSA Go
package main import "fmt" func countingSort(arr []int) []int { max := arr[0] for _, v := range arr { if v > max { max = v } } count := make([]int, max+1) for _, v := range arr { count[v]++ } index := 0 for i, c := range count { for c > 0 { arr[index] = i index++ c-- } } return arr } func main() { arr := []int{4, 2, 2, 8, 3, 3, 1} sorted := countingSort(arr) fmt.Println(sorted) }
Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then rebuilds the array in order.
✗ Incorrect
The counting sort algorithm counts the frequency of each number and then places them back in sorted order. The output is the sorted array [1 2 2 3 3 4 8].
🧠 Conceptual
intermediate1:30remaining
Counting Sort Stability Property
Which statement correctly 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 the output array in the order they appear in the input, preserving relative order of duplicates.
🔧 Debug
advanced2:00remaining
Identify the bug in this counting sort implementation
What error will the following Go code produce when run, and why?
func countingSort(arr []int) []int {
max := 0
for _, v := range arr {
if v > max {
max = v
}
}
count := make([]int, max)
for _, v := range arr {
count[v]++
}
index := 0
for i, c := range count {
for c > 0 {
arr[index] = i
index++
c--
}
}
return arr
}
Attempts:
2 left
💡 Hint
Check how the count slice size relates to the max value in the array.
✗ Incorrect
The count slice is created with length max, but since array indices go from 0 to max, it should be max+1. Accessing count[max] causes index out of range panic.
🚀 Application
advanced1:30remaining
Counting Sort Usage Scenario
Which scenario is best suited for using counting sort instead of comparison-based sorting algorithms?
Attempts:
2 left
💡 Hint
Counting sort works best when the range of input values is not too large.
✗ Incorrect
Counting sort is efficient when the range of input values is small relative to the number of elements, allowing linear time sorting.
❓ Predict Output
expert2:00remaining
Output of Counting Sort with Negative Numbers
What will happen if you run the following Go code that tries to apply counting sort on an array containing negative numbers?
arr := []int{3, -1, 2, -1, 0}
sorted := countingSort(arr)
fmt.Println(sorted)
Assume countingSort is the standard implementation that uses the maximum value to create the count array size.
Attempts:
2 left
💡 Hint
Counting sort uses array indices for counts, which cannot be negative.
✗ Incorrect
Counting sort does not handle negative numbers because it uses array indices to count occurrences. Negative numbers cause invalid negative indices, leading to runtime panic.