Challenge - 5 Problems
Selection Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Selection Sort on a small array
What is the output of the following Go code after running selection sort on the array?
DSA Go
package main import "fmt" func selectionSort(arr []int) { n := len(arr) for i := 0; i < n-1; i++ { minIdx := i for j := i + 1; j < n; j++ { if arr[j] < arr[minIdx] { minIdx = j } } arr[i], arr[minIdx] = arr[minIdx], arr[i] } } func main() { arr := []int{64, 25, 12, 22, 11} selectionSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and puts it at the start, then repeats for the rest.
✗ Incorrect
Selection sort repeatedly selects the smallest element from the unsorted part and swaps it with the first unsorted element. After sorting, the array is in ascending order.
🧠 Conceptual
intermediate1:30remaining
Number of swaps in Selection Sort
How many swaps does selection sort perform on an array of length n in the worst case?
Attempts:
2 left
💡 Hint
Selection sort swaps only once per outer loop iteration.
✗ Incorrect
Selection sort performs exactly one swap per iteration of the outer loop, so total swaps are n - 1.
🔧 Debug
advanced2:00remaining
Identify the error in this selection sort implementation
What error will this Go code produce when running selection sort?
DSA Go
package main import "fmt" func selectionSort(arr []int) { n := len(arr) for i := 0; i <= n-1; i++ { minIdx := i for j := i + 1; j < n; j++ { if arr[j] < arr[minIdx] { minIdx = j } } arr[i], arr[minIdx] = arr[minIdx], arr[i] } } func main() { arr := []int{3, 1, 2} selectionSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the loop conditions carefully.
✗ Incorrect
The loop condition i <= n-1 is valid because i goes from 0 to n-1 inclusive. The code runs correctly and sorts the array.
❓ Predict Output
advanced2:00remaining
Selection Sort on a reversed array
What is the output of the following Go code after sorting the reversed array?
DSA Go
package main import "fmt" func selectionSort(arr []int) { n := len(arr) for i := 0; i < n-1; i++ { minIdx := i for j := i + 1; j < n; j++ { if arr[j] < arr[minIdx] { minIdx = j } } arr[i], arr[minIdx] = arr[minIdx], arr[i] } } func main() { arr := []int{5, 4, 3, 2, 1} selectionSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Selection sort always sorts ascending regardless of input order.
✗ Incorrect
Selection sort finds the smallest element and places it at the front, so the reversed array becomes sorted ascending.
🧠 Conceptual
expert1:30remaining
Time complexity of Selection Sort
What is the time complexity of selection sort in all cases?
Attempts:
2 left
💡 Hint
Selection sort always scans the entire unsorted part to find the minimum.
✗ Incorrect
Selection sort always performs n*(n-1)/2 comparisons regardless of input order, so time complexity is O(n^2) in all cases.