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 using the last element as pivot on this array?
Initial array: [8, 3, 7, 6, 2]
Pivot: last element (2)
Initial array: [8, 3, 7, 6, 2]
Pivot: last element (2)
DSA C
int arr[] = {8, 3, 7, 6, 2}; int pivot = arr[4]; int i = -1; for (int j = 0; j < 4; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } i++; int temp = arr[i]; arr[i] = arr[4]; arr[4] = temp; // Print arr
Attempts:
2 left
š” Hint
Remember, elements less than pivot move to the left, pivot placed after them.
ā Incorrect
The partition moves elements smaller than 2 to the left. Since only 2 is smallest, it swaps with first element. Result: [2, 3, 7, 6, 8].
ā Predict Output
intermediateFinal Sorted Array by Quick Sort
What is the final sorted array after applying Quick Sort on this array?
Input array: [4, 1, 9, 7, 3]
Input array: [4, 1, 9, 7, 3]
DSA C
int arr[] = {4, 1, 9, 7, 3}; // Quick sort applied fully // Print arr
Attempts:
2 left
š” Hint
Quick Sort sorts in ascending order.
ā Incorrect
Quick Sort sorts the array fully in ascending order: [1, 3, 4, 7, 9].
š§ Debug
advancedIdentify the Error in Quick Sort Partition Code
What error will this Quick Sort partition code cause when run?
Code snippet:
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++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i]; arr[i] = arr[high]; arr[high] = temp; return i; }
Code snippet:
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++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i]; arr[i] = arr[high]; arr[high] = temp; return i; }
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++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i]; arr[i] = arr[high]; arr[high] = temp; return i; }
Attempts:
2 left
š” Hint
Check the swap of pivot with arr[i].
ā Incorrect
The pivot swap uses arr[i], but i points to last smaller element, pivot should swap with arr[i+1]. This causes wrong partition.
š§ Conceptual
advancedQuick Sort Worst Case Scenario
Which input array causes the worst-case time complexity for Quick Sort when using the last element as pivot?
Attempts:
2 left
š” Hint
Worst case happens when pivot divides array unevenly.
ā Incorrect
Using last element as pivot, an already sorted array causes unbalanced partitions leading to O(n²) time.
š Application
expertNumber of Recursive Calls in Quick Sort
Given the array [5, 3, 8, 4, 2] sorted by Quick Sort using last element as pivot, how many total recursive calls (including the initial call) are made?
Attempts:
2 left
š” Hint
Count calls for each partition step carefully.
ā Incorrect
The partitioning splits the array into subarrays recursively. Counting all calls including base cases gives 9.
