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 Array
What is the output array after applying Lomuto partition on the given array with pivot as last element?
DSA Java
int[] arr = {8, 3, 7, 6, 2}; int pivotIndex = lomutoPartition(arr, 0, arr.length - 1); System.out.print(java.util.Arrays.toString(arr)); System.out.print(", pivotIndex=" + pivotIndex); public static int lomutoPartition(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; }
Attempts:
2 left
💡 Hint
Remember Lomuto places pivot at correct position with smaller elements to left.
✗ Incorrect
Lomuto partition moves all elements smaller or equal to pivot to left, then places pivot after them. Here pivot=2, so after partition, 2 is at index 0 with smaller or equal elements before it.
❓ Predict Output
intermediateOutput of Hoare Partition on Array
What is the output array and returned pivot index after applying Hoare partition on the array?
DSA Java
int[] arr = {9, 4, 8, 3, 7}; int pivotIndex = hoarePartition(arr, 0, arr.length - 1); System.out.print(java.util.Arrays.toString(arr)); System.out.print(", pivotIndex=" + pivotIndex); public static int hoarePartition(int[] arr, int low, int high) { int pivot = arr[low]; int i = low - 1; int j = high + 1; while (true) { 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; } }
Attempts:
2 left
💡 Hint
Hoare partition uses first element as pivot and swaps elements crossing pointers.
✗ Incorrect
Hoare partition swaps elements to ensure left side less than pivot and right side greater. The pivot is arr[0]=9, after partition smaller elements move left. The returned index is where pointers cross.
🔧 Debug
advancedIdentify the error in Lomuto partition code snippet
Which option correctly identifies the error in this Lomuto partition code snippet?
DSA Java
int lomutoPartition(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 how i is initialized and incremented in Lomuto partition.
✗ Incorrect
In Lomuto partition, i starts at low - 1 to track last smaller element index. Starting at low causes incorrect partitioning.
❓ Predict Output
advancedResult of Hoare Partition on Sorted Array
What is the output array and pivot index after applying Hoare partition on a sorted array?
DSA Java
int[] arr = {1, 2, 3, 4, 5}; int pivotIndex = hoarePartition(arr, 0, arr.length - 1); System.out.print(java.util.Arrays.toString(arr)); System.out.print(", pivotIndex=" + pivotIndex); public static int hoarePartition(int[] arr, int low, int high) { int pivot = arr[low]; int i = low - 1; int j = high + 1; while (true) { 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; } }
Attempts:
2 left
💡 Hint
Hoare partition returns index where pointers cross, pivot is first element.
✗ Incorrect
Since array is sorted and pivot is smallest element, pointers cross immediately at low index 0, no swaps occur.
🧠 Conceptual
expertDifference in Pivot Placement Between Lomuto and Hoare Partition
Which statement correctly describes the difference in pivot placement between Lomuto and Hoare partition schemes?
Attempts:
2 left
💡 Hint
Think about where the pivot ends after partition in each scheme.
✗ Incorrect
Lomuto partition always places pivot at its correct sorted position. Hoare partition only ensures elements are partitioned but pivot may not be at final position.
