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 on a small list
What is the output of the following Python code after sorting the list using Quick Sort?
DSA Python
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) print(quick_sort([3, 6, 8, 10, 1, 2, 1]))
Attempts:
2 left
💡 Hint
Remember Quick Sort divides the list around a pivot and sorts recursively.
✗ Incorrect
The code picks the middle element as pivot and recursively sorts elements less than and greater than the pivot. The final output is the sorted list in ascending order.
🧠 Conceptual
intermediateUnderstanding Quick Sort Partitioning
In Quick Sort, what is the main purpose of the partition step?
Attempts:
2 left
💡 Hint
Think about how Quick Sort organizes elements around the pivot.
✗ Incorrect
Partitioning rearranges elements so that those smaller than the pivot come before it and those larger come after, enabling recursive sorting.
🔧 Debug
advancedIdentify the error in this Quick Sort implementation
What error will this Quick Sort code produce when run?
DSA Python
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [x for x in arr if x < pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + [pivot] + quick_sort(right) print(quick_sort([3, 6, 8, 10, 1, 2, 1]))
Attempts:
2 left
💡 Hint
Check how elements equal to the pivot are handled in the list comprehensions.
✗ Incorrect
Elements equal to the pivot are excluded from both left and right lists, causing duplicate elements equal to the pivot to be lost.
❓ Predict Output
advancedOutput of Quick Sort with duplicates and negative numbers
What is the output of this Quick Sort code?
DSA Python
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[-1] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) print(quick_sort([0, -1, 5, 3, -1, 5, 2]))
Attempts:
2 left
💡 Hint
Remember the pivot is the last element and duplicates are handled in the middle list.
✗ Incorrect
The code sorts the list including duplicates and negative numbers correctly in ascending order.
🧠 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 causing unbalanced partitions and O(n^2) time.
