Challenge - 5 Problems
Quick Sort Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Lomuto Partition on given array
What is the output index returned by the Lomuto partition function for the array below?
Array: [8, 3, 7, 6, 2, 5, 4, 1]
Pivot is the last element.
Array: [8, 3, 7, 6, 2, 5, 4, 1]
Pivot is the last element.
DSA C
int lomuto_partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } int main() { int arr[] = {8, 3, 7, 6, 2, 5, 4, 1}; int n = sizeof(arr) / sizeof(arr[0]); int p = lomuto_partition(arr, 0, n - 1); printf("%d", p); return 0; }
Attempts:
2 left
💡 Hint
Remember Lomuto places pivot at its correct position after partitioning.
✗ Incorrect
Lomuto partition moves elements <= pivot to the left and finally places pivot after them. The pivot 1 ends up at index 0 after swaps.
❓ Predict Output
intermediateOutput of Hoare Partition on given array
What is the index returned by the Hoare partition function for the array below?
Array: [9, 4, 8, 3, 7, 6, 2, 5]
Pivot is the first element.
Array: [9, 4, 8, 3, 7, 6, 2, 5]
Pivot is the first element.
DSA C
int hoare_partition(int arr[], int low, int high) { int pivot = arr[low]; int i = low - 1; int j = high + 1; while (1) { do { i++; } while (arr[i] < pivot); do { j--; } while (arr[j] > pivot); if (i >= j) return j; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int main() { int arr[] = {9, 4, 8, 3, 7, 6, 2, 5}; int n = sizeof(arr) / sizeof(arr[0]); int p = hoare_partition(arr, 0, n - 1); printf("%d", p); return 0; }
Attempts:
2 left
💡 Hint
Hoare partition returns the index where left and right pointers cross.
✗ Incorrect
Hoare partition uses two pointers moving inward. The returned index is where pointers cross, here index 3.
🧠 Conceptual
advancedDifference in pivot placement between Lomuto and Hoare partitions
Which statement correctly describes the difference in pivot placement after Lomuto and Hoare partition schemes?
Attempts:
2 left
💡 Hint
Think about where the pivot ends after partitioning in each scheme.
✗ Incorrect
Lomuto partition always places pivot at its correct sorted index. Hoare partition only ensures elements are partitioned but pivot may not be at final position.
🔧 Debug
advancedIdentify the bug in this Lomuto partition code snippet
What error will this Lomuto partition code produce when run on array [5, 3, 8, 4, 2]?
DSA C
int lomuto_partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; } } int temp = arr[i]; arr[i] = arr[high]; arr[high] = temp; return i; }
Attempts:
2 left
💡 Hint
Check initial value of i and how it affects swapping.
✗ Incorrect
In Lomuto partition, i must start at low - 1 to correctly track elements <= pivot. Starting at low causes incorrect partitioning.
🚀 Application
expertNumber of recursive calls in Quick Sort using Hoare partition
Given an array of 7 distinct elements sorted in descending order, how many recursive calls will Quick Sort make using Hoare partition scheme (excluding the initial call)?
Attempts:
2 left
💡 Hint
Consider worst-case recursion depth and number of calls in Quick Sort with Hoare partition on reverse sorted array.
✗ Incorrect
In worst case (reverse sorted), Quick Sort with Hoare partition makes 2n - 1 calls total. For n=7, total calls = 13, so excluding initial call, recursive calls = 13 - 1 = 12.
