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 this Quick Sort code snippet?
DSA C++
int 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++; std::swap(arr[i], arr[j]); } } std::swap(arr[i + 1], arr[high]); return i + 1; } int main() { int arr[] = {10, 7, 8, 9, 1, 5}; int n = 6; int pi = partition(arr, 0, n - 1); for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Remember the pivot is the last element and partition places it in the correct position.
✗ Incorrect
The partition function uses the last element as pivot (5). After partition, all elements less than 5 are before it. The pivot 5 ends at index 2, so the array is rearranged as [1, 5, 7, 8, 9, 10].
🧠 Conceptual
intermediateUnderstanding Quick Sort's Divide and Conquer
Which statement best describes the divide and conquer approach in Quick Sort?
Attempts:
2 left
💡 Hint
Think about how Quick Sort breaks down the problem into smaller parts.
✗ Incorrect
Quick Sort picks a pivot, partitions the array into elements less than and greater than the pivot, then recursively sorts these parts. This is the divide and conquer strategy.
🔧 Debug
advancedIdentify the Bug in Quick Sort Partition
What error will this Quick Sort partition code cause when run?
DSA C++
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) { std::swap(arr[i], arr[j]); i++; } } std::swap(arr[i], arr[high]); return i; }
Attempts:
2 left
💡 Hint
Check the initial value of i and how it moves.
✗ Incorrect
The variable i starts at low instead of low - 1, causing the partition to swap incorrectly and produce wrong partitioning results.
❓ Predict Output
advancedFinal Sorted Array After Quick Sort
What is the final sorted array after running Quick Sort on this input?
DSA C++
void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int 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++; std::swap(arr[i], arr[j]); } } std::swap(arr[i + 1], arr[high]); return i + 1; } int main() { int arr[] = {4, 2, 6, 9, 3}; int n = 5; quickSort(arr, 0, n - 1); for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
✗ Incorrect
Quick Sort correctly sorts the array into ascending order: 2, 3, 4, 6, 9.
🧠 Conceptual
expertTime Complexity of Quick Sort in Worst Case
What is the worst-case time complexity of Quick Sort and when does it occur?
Attempts:
2 left
💡 Hint
Think about how unbalanced partitions affect recursion depth.
✗ Incorrect
Worst case happens when pivot picks smallest or largest element repeatedly, causing unbalanced partitions and O(n^2) time.
