Challenge - 5 Problems
Quick Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Quick Sort Partition Step
What is the output of the partition function after running on the array [4, 2, 7, 1, 3] with pivot as the last element?
DSA Go
func partition(arr []int, low, high int) int { pivot := arr[high] i := low - 1 for j := low; j < high; j++ { if arr[j] < pivot { i++ arr[i], arr[j] = arr[j], arr[i] } } arr[i+1], arr[high] = arr[high], arr[i+1] return i + 1 } arr := []int{4, 2, 7, 1, 3} pos := partition(arr, 0, len(arr)-1) fmt.Println(arr, pos)
Attempts:
2 left
💡 Hint
Remember the pivot is the last element, and elements less than pivot move to the left.
✗ Incorrect
The partition function moves all elements less than pivot (3) to the left and places pivot after them. The final array is [2, 1, 3, 4, 7] with pivot index 2.
🧠 Conceptual
intermediateUnderstanding Quick Sort Recursive Calls
In Quick Sort, after partitioning the array, which parts are sorted recursively?
Attempts:
2 left
💡 Hint
Think about where the pivot ends up after partitioning.
✗ Incorrect
After partitioning, the pivot is in its correct position. Quick Sort recursively sorts the left and right sub-arrays excluding the pivot.
🔧 Debug
advancedIdentify the Bug in Quick Sort Partition
What error does this partition function cause when run on array [5, 3, 8, 4, 2]?
func partition(arr []int, low, high int) int {
pivot := arr[low]
i := low + 1
for j := low + 1; j <= high; j++ {
if arr[j] < pivot {
arr[i], arr[j] = arr[j], arr[i]
i++
}
}
arr[low], arr[i-1] = arr[i-1], arr[low]
return i - 1
}
DSA Go
func partition(arr []int, low, high int) int { pivot := arr[low] i := low + 1 for j := low + 1; j <= high; j++ { if arr[j] < pivot { arr[i], arr[j] = arr[j], arr[i] i++ } } arr[low], arr[i-1] = arr[i-1], arr[low] return i - 1 } arr := []int{5, 3, 8, 4, 2} pos := partition(arr, 0, len(arr)-1) fmt.Println(arr, pos)
Attempts:
2 left
💡 Hint
Check how pivot is chosen and swapped.
✗ Incorrect
No error. The partition function correctly rearranges the array to [2, 3, 4, 5, 8] with pivot at index 3.
📝 Syntax
advancedSyntax Error in Quick Sort Implementation
Which option contains a syntax error in this Quick Sort snippet?
DSA Go
func quickSort(arr []int, low, high int) { if low < high { pi := partition(arr, low, high) quickSort(arr, low, pi-1) quickSort(arr, pi+1, high) } }
Attempts:
2 left
💡 Hint
Check for missing closing braces.
✗ Incorrect
Option A is missing the closing brace for the function, causing a syntax error.
🚀 Application
expertQuick Sort Performance on Nearly Sorted Array
What is the time complexity of Quick Sort on a nearly sorted array if the pivot is always chosen as the first element?
Attempts:
2 left
💡 Hint
Consider how pivot choice affects partitioning on sorted data.
✗ Incorrect
Choosing the first element as pivot on nearly sorted arrays causes unbalanced partitions, leading to worst-case O(n^2) time complexity.
