Challenge - 5 Problems
Quick Sort Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Lomuto Partition on Array
What is the output array after applying Lomuto partition on the given array with pivot as last element?
DSA Go
func lomutoPartition(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 } func main() { arr := []int{8, 3, 7, 6, 2, 5, 4, 1} lomutoPartition(arr, 0, len(arr)-1) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Remember Lomuto places pivot at correct position and partitions smaller elements to left.
✗ Incorrect
Lomuto partition uses last element as pivot. It moves elements smaller or equal to pivot to left and finally places pivot after them. The resulting array after partition is [1 3 7 6 2 5 4 8].
❓ Predict Output
intermediate2:00remaining
Output of Hoare Partition on Array
What is the output array after applying Hoare partition on the given array with pivot as first element?
DSA Go
func hoarePartition(arr []int, low, high int) int { pivot := arr[low] i := low - 1 j := high + 1 for { for { i++ if arr[i] >= pivot { break } } for { j-- if arr[j] <= pivot { break } } if i >= j { return j } arr[i], arr[j] = arr[j], arr[i] } } func main() { arr := []int{8, 3, 7, 6, 2, 5, 4, 1} hoarePartition(arr, 0, len(arr)-1) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Hoare partition uses first element as pivot and swaps elements crossing from left and right.
✗ Incorrect
Hoare partition picks first element as pivot (8). It swaps elements to ensure left side <= pivot and right side >= pivot. The resulting array after partition is [1 3 7 6 2 5 4 8].
🧠 Conceptual
advanced1:30remaining
Difference Between Lomuto and Hoare Partition
Which statement correctly describes a key difference between Lomuto and Hoare partition schemes?
Attempts:
2 left
💡 Hint
Think about where the pivot ends after partitioning in each scheme.
✗ Incorrect
Lomuto partition places the pivot at its correct sorted position after partitioning. Hoare partition does not guarantee the pivot is at its final position.
🔧 Debug
advanced2:00remaining
Identify the Bug in Lomuto Partition Code
Given the Lomuto partition code below, which option correctly identifies the bug causing incorrect partitioning?
DSA Go
func lomutoPartition(arr []int, low, high int) int { pivot := arr[high] i := low for j := low; j < high; j++ { if arr[j] < pivot { arr[i], arr[j] = arr[j], arr[i] i++ } } arr[i], arr[high] = arr[high], arr[i] return i }
Attempts:
2 left
💡 Hint
Check how 'i' is initialized and how it tracks the boundary of smaller elements.
✗ Incorrect
In Lomuto partition, 'i' must start at 'low - 1' so that when the first smaller element is found, 'i' increments to 'low' and swaps correctly. Starting at 'low' skips the first element.
🚀 Application
expert2:30remaining
Choosing Partition Scheme for Nearly Sorted Array
You have a nearly sorted large array. Which partition scheme is generally better to reduce worst-case performance in Quick Sort?
Attempts:
2 left
💡 Hint
Consider pivot selection and partition scheme impact on nearly sorted data.
✗ Incorrect
Hoare partition combined with median-of-three pivot selection helps avoid worst-case O(n^2) on nearly sorted arrays by choosing better pivots and balanced partitions.