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 the array [4, 7, 2, 9, 1]?
DSA C#
int[] arr = {4, 7, 2, 9, 1}; int pivot = arr[arr.Length - 1]; int i = -1; for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } i++; int tempPivot = arr[i]; arr[i] = arr[arr.Length - 1]; arr[arr.Length - 1] = tempPivot; Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Remember the pivot is the last element and elements smaller than pivot move to the left.
✗ Incorrect
The partition step moves all elements smaller than the pivot (1) to the left. After swapping, the array becomes [1, 7, 2, 9, 4].
❓ Predict Output
intermediateFinal Sorted Array After Quick Sort
What is the final sorted array after applying Quick Sort on [3, 6, 8, 10, 1, 2, 1]?
DSA C#
int[] arr = {3, 6, 8, 10, 1, 2, 1}; QuickSort(arr, 0, arr.Length - 1); Console.WriteLine(string.Join(", ", arr)); void QuickSort(int[] array, int low, int high) { if (low < high) { int pi = Partition(array, low, high); QuickSort(array, low, pi - 1); QuickSort(array, pi + 1, high); } } int Partition(int[] array, int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] <= pivot) { i++; int temp = array[i]; array[i] = array[j]; array[j] = temp; } } int temp1 = array[i + 1]; array[i + 1] = array[high]; array[high] = temp1; return i + 1; }
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
✗ Incorrect
The Quick Sort algorithm sorts the array in ascending order. The final sorted array is [1, 1, 2, 3, 6, 8, 10].
🔧 Debug
advancedIdentify the Bug in Quick Sort Partition Function
What error will the following Quick Sort partition function cause when run on any array?
DSA C#
int Partition(int[] arr, int low, int high) { int pivot = arr[low]; int i = low + 1; int j = high; while (true) { while (i <= j && arr[i] <= pivot) i++; while (i <= j && arr[j] > pivot) j--; if (i > j) break; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } int temp2 = arr[low]; arr[low] = arr[j]; arr[j] = temp2; return j; }
Attempts:
2 left
💡 Hint
Check the inner while loops and their boundary conditions.
✗ Incorrect
The inner while loops can increment i or decrement j beyond array bounds if conditions are not carefully checked, causing IndexOutOfRangeException.
🧠 Conceptual
advancedTime 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
Worst case happens when partitioning is unbalanced.
✗ Incorrect
The worst-case time complexity is O(n^2) and occurs when the pivot is always the smallest or largest element, causing unbalanced partitions.
🚀 Application
expertQuick Sort Stability Check
Given the array of objects [{value: 4, id: 'a'}, {value: 4, id: 'b'}, {value: 3, id: 'c'}], which statement about Quick Sort stability is true?
Attempts:
2 left
💡 Hint
Think about how swapping equal elements affects order.
✗ Incorrect
Quick Sort is generally unstable because swapping can change the relative order of equal elements.
