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 partition function after running on the array [4, 2, 7, 1, 3] with pivot as the last element?
DSA Rust
fn partition(arr: &mut [i32]) -> usize {
let pivot = arr[arr.len() - 1];
let mut i = 0;
for j in 0..arr.len() - 1 {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, arr.len() - 1);
i
}
fn main() {
let mut arr = [4, 2, 7, 1, 3];
let p = partition(&mut arr);
println!("Partition index: {}", p);
println!("Array after partition: {:?}", arr);
}Attempts:
2 left
š” Hint
Remember the pivot is the last element and elements less than pivot are moved to the left.
ā Incorrect
The pivot is 3. Elements less than 3 are 2 and 1, which are moved to the left. The pivot is placed at index 2 after swapping. The array becomes [2, 1, 3, 4, 7].
š§ Conceptual
intermediateUnderstanding Quick Sort's Divide and Conquer
Which statement best describes the divide and conquer approach in Quick Sort?
Attempts:
2 left
š” Hint
Think about how Quick Sort breaks down the problem into smaller parts.
ā Incorrect
Quick Sort picks a pivot, partitions the array into elements less than and greater than the pivot, then recursively sorts these parts.
š§ Debug
advancedIdentify the Bug in Quick Sort Implementation
What error will this Rust Quick Sort code produce when sorting [3, 1, 4]?
fn quick_sort(arr: &mut [i32]) {
if arr.len() <= 1 {
return;
}
let pivot_index = arr.len() / 2;
let pivot = arr[pivot_index];
let mut left = 0;
let mut right = arr.len() - 1;
while left <= right {
while arr[left] < pivot {
left += 1;
}
while arr[right] > pivot {
right -= 1;
}
if left <= right {
arr.swap(left, right);
left += 1;
right -= 1;
}
}
quick_sort(&mut arr[0..right]);
quick_sort(&mut arr[left..arr.len()]);
}
Attempts:
2 left
š” Hint
Check what happens when right becomes negative and is used as usize index.
ā Incorrect
When right becomes -1 and is cast to usize, it becomes a very large number causing out of bounds slicing panic.
š Syntax
advancedCorrect Rust Syntax for Recursive Quick Sort Call
Which option correctly calls quick_sort recursively on the left partition of the array slice arr[0..pivot_index] in Rust?
Attempts:
2 left
š” Hint
Remember Rust slice syntax and mutable references.
ā Incorrect
Rust slices use .. for ranges and &mut for mutable references. Option D uses both correctly.
š Application
expertFinal Sorted Array After Full Quick Sort
Given the Rust quick_sort function below, what is the final sorted array after calling quick_sort on [5, 3, 8, 4, 2]?
fn quick_sort(arr: &mut [i32]) {
if arr.len() <= 1 {
return;
}
let pivot_index = arr.len() / 2;
let pivot = arr[pivot_index];
let mut left = 0;
let mut right = arr.len() - 1;
while left <= right {
while arr[left] < pivot {
left += 1;
}
while arr[right] > pivot {
right -= 1;
}
if left <= right {
arr.swap(left, right);
left += 1;
right -= 1;
}
}
quick_sort(&mut arr[0..=right]);
quick_sort(&mut arr[left..arr.len()]);
}
Attempts:
2 left
š” Hint
Trace the partitioning and recursive calls carefully.
ā Incorrect
The function correctly partitions and recursively sorts both sides, resulting in a fully sorted array [2, 3, 4, 5, 8].
