0
0
DSA Goprogramming~20 mins

Counting Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Counting Sort Mastery
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 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)
}
A[1 2 3 3 4 8 8]
B[4 2 2 8 3 3 1]
C[8 4 3 3 2 2 1]
D[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 property of counting sort?
ACounting sort is stable only if the input array is already sorted.
BCounting sort is unstable because it rearranges equal elements randomly.
CCounting sort is stable because it preserves the relative order of equal elements.
DCounting sort is unstable because it uses a hash map internally.
Attempts:
2 left
💡 Hint
Stability means equal elements keep their original order after sorting.
🔧 Debug
advanced
2: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 }
ARuntime panic: nil pointer dereference
BRuntime panic: index out of range because count slice size is max instead of max+1
CCompilation error due to missing import statement
DNo error, outputs sorted array correctly
Attempts:
2 left
💡 Hint
Check how the count slice size relates to the max value in the array.
🚀 Application
advanced
1:30remaining
Counting Sort Usage Scenario
Which scenario is best suited for using counting sort instead of comparison-based sorting algorithms?
ASorting a large list of integers where the range of values is small compared to the list size.
BSorting a list of integers where the range is very large compared to the list size.
CSorting a list of strings alphabetically.
DSorting a list of floating-point numbers with many decimal places.
Attempts:
2 left
💡 Hint
Counting sort works best when the range of input values is not too large.
Predict Output
expert
2: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.
ARuntime panic: index out of range due to negative index access in count array
B[ -1 -1 0 2 3 ]
C[ 0 2 3 -1 -1 ]
DCompilation error due to negative numbers
Attempts:
2 left
💡 Hint
Counting sort uses array indices for counts, which cannot be negative.