Challenge - 5 Problems
Quick Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Quick Sort Partition Step
What is the state of the array after the partition step if the pivot is chosen as the last element?
DSA C
int arr[] = {8, 3, 7, 6, 2}; int low = 0, high = 4; 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; // Print array state for (int k = 0; k <= high; k++) { printf("%d ", arr[k]); }
Attempts:
2 left
💡 Hint
Remember the partition step moves elements smaller than pivot to the left.
✗ Incorrect
The partition step places all elements smaller than pivot (2) before it. Since 2 is the smallest element, it moves to index 0, resulting in 2 3 7 6 8. However, 3, 7, and 6 remain in their relative order after partitioning, so the array after partition is 2 3 7 6 8. But since 3, 6, and 7 are not all smaller than pivot, the correct partition places 2 at the start and the rest after it. The correct final array after partition is 2 3 7 6 8, which matches option A.
🧠 Conceptual
intermediate1:30remaining
Understanding Quick Sort's Divide and Conquer
Which statement best describes the divide and conquer approach in Quick Sort?
Attempts:
2 left
💡 Hint
Focus on how Quick Sort uses a pivot to split the array.
✗ Incorrect
Quick Sort picks a pivot, partitions the array so smaller elements go left, larger go right, then sorts partitions recursively.
🔧 Debug
advanced2:30remaining
Identify the Bug in Quick Sort Partition Code
What error will this 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) { 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 variable i is initialized and incremented.
✗ Incorrect
Variable i should start at low - 1 to correctly track smaller elements; starting at low causes wrong partition index.
❓ Predict Output
advanced2:00remaining
Final Sorted Array After Quick Sort
What is the final sorted array after applying Quick Sort on this input?
DSA C
int arr[] = {10, 7, 8, 9, 1, 5}; int n = 6; 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); } } // Assume partition is correctly implemented quickSort(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
✗ Incorrect
Quick Sort sorts the array fully in ascending order: 1 5 7 8 9 10.
🧠 Conceptual
expert1:30remaining
Quick Sort Worst Case Scenario
Which input array causes Quick Sort to have its worst-case time complexity?
Attempts:
2 left
💡 Hint
Worst case happens when partitioning is unbalanced.
✗ Incorrect
Choosing last element as pivot on sorted array causes unbalanced partitions, leading to O(n^2) time.