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 of the array after the partition step in this Quick Sort code snippet?
DSA Javascript
function partition(arr, low, high) {
let pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
}
let array = [8, 3, 7, 6, 2, 5, 4];
let pivotIndex = partition(array, 0, array.length - 1);
console.log(array);Attempts:
2 left
💡 Hint
Focus on how elements smaller than the pivot are moved before it.
✗ Incorrect
The partition function moves all elements smaller than the pivot (8) to the left and places the pivot after them. So the pivot 8 ends up at the last position, and smaller elements are before it but not fully sorted.
❓ Predict Output
intermediateFinal Sorted Array from Quick Sort
What is the output of the array after running the full Quick Sort on this input?
DSA Javascript
function quickSort(arr, low, high) {
if (low < high) {
let pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
function partition(arr, low, high) {
let pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
}
let array = [10, 7, 8, 9, 1, 5];
quickSort(array, 0, array.length - 1);
console.log(array);Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
✗ Incorrect
Quick Sort recursively partitions and sorts the array. The final output is the fully sorted array in ascending order.
🧠 Conceptual
advancedUnderstanding Quick Sort Pivot Choice Impact
Which statement best explains how the choice of pivot affects Quick Sort's performance?
Attempts:
2 left
💡 Hint
Think about how balanced partitions affect recursion depth.
✗ Incorrect
Quick Sort performs best when the pivot divides the array into two roughly equal parts, minimizing recursion depth and comparisons.
🔧 Debug
advancedIdentify the Bug in Quick Sort Partition
What error will this Quick Sort partition function cause when run?
DSA Javascript
function partition(arr, low, high) {
let pivot = arr[low];
let i = low + 1;
for (let j = low + 1; j <= high; j++) {
if (arr[j] < pivot) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
[arr[low], arr[i - 1]] = [arr[i - 1], arr[low]];
return i - 1;
}
let array = [4, 5, 3, 7, 2];
let pi = partition(array, 0, array.length - 1);
console.log(array);Attempts:
2 left
💡 Hint
Check the final swap indices carefully.
✗ Incorrect
The final swap uses arr[i] but i points one past the last smaller element, causing pivot to be placed incorrectly.
🚀 Application
expertQuick Sort on Linked List
Which approach is best to implement Quick Sort on a singly linked list?
Attempts:
2 left
💡 Hint
Think about how to rearrange nodes efficiently in a linked list.
✗ Incorrect
Quick Sort can be done in-place on linked lists by choosing a pivot and rearranging nodes by changing pointers, avoiding extra space.
