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 array after the partition step in Quick Sort for the given array and pivot?
DSA Java
int[] arr = {9, 3, 7, 6, 2, 8}; int pivot = arr[arr.length - 1]; int i = -1; for (int j = 0; j < arr.length - 1; 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[arr.length - 1]; arr[arr.length - 1] = temp; System.out.println(java.util.Arrays.toString(arr));
Attempts:
2 left
💡 Hint
Focus on how elements less than or equal to the pivot are moved to the left.
✗ Incorrect
The partition step moves all elements less than or equal to the pivot (8) to the left side, then places the pivot after them. The resulting array is [3, 7, 6, 2, 8, 9].
🧠 Conceptual
intermediateChoosing Pivot in Quick Sort
Which of the following pivot choices can cause Quick Sort to degrade to worst-case performance on an already sorted array?
Attempts:
2 left
💡 Hint
Think about how the pivot divides the array in sorted input.
✗ Incorrect
Choosing the first element as pivot on a sorted array causes unbalanced partitions, leading to O(n^2) time.
🔧 Debug
advancedIdentify the Bug in Quick Sort Partition Code
What error will the following Quick Sort partition code produce when run?
DSA Java
int 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 the initialization of i and the loop boundaries carefully.
✗ Incorrect
The code correctly partitions the array and returns the pivot index without errors.
❓ Predict Output
advancedFinal Sorted Array After Quick Sort
What is the final sorted array after applying Quick Sort on the array below?
DSA Java
int[] arr = {5, 1, 4, 2, 8}; // Assume standard Quick Sort implementation is applied // Output the sorted array
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
✗ Incorrect
Quick Sort sorts the array fully in ascending order: [1, 2, 4, 5, 8].
🧠 Conceptual
expertTime Complexity of Quick Sort in Different Cases
Which statement correctly describes the time complexity of Quick Sort?
Attempts:
2 left
💡 Hint
Consider how balanced partitions affect complexity.
✗ Incorrect
Quick Sort average and best cases are O(n log n) when partitions are balanced; worst case O(n^2) when partitions are unbalanced.
