Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sorting and Searching in Rust
What is the printed output of this Rust code that sorts a vector and then searches for a value?
DSA Rust
fn main() {
let mut numbers = vec![10, 3, 5, 8, 2];
numbers.sort();
let pos = numbers.binary_search(&5);
println!("Sorted: {:?}, Position: {:?}", numbers, pos);
}Attempts:
2 left
💡 Hint
Remember that sort() arranges numbers in ascending order and binary_search returns Ok(index) if found.
✗ Incorrect
The vector is sorted ascending: [2, 3, 5, 8, 10]. The number 5 is at index 2, so binary_search returns Ok(2).
🧠 Conceptual
intermediateWhy Sorting Helps in Finding Duplicates
Why is sorting a useful first step when trying to find duplicates in a list?
Attempts:
2 left
💡 Hint
Think about how duplicates appear after sorting.
✗ Incorrect
Sorting places equal elements next to each other, so duplicates appear as neighbors and can be found by simple adjacent comparisons.
🔧 Debug
advancedWhy Does This Merge Sort Code Fail?
This Rust code tries to merge two sorted slices but produces wrong output. What is the bug?
DSA Rust
fn merge(left: &[i32], right: &[i32]) -> Vec<i32> {
let mut result = Vec::new();
let mut i = 0;
let mut j = 0;
while i < left.len() && j < right.len() {
if left[i] < right[j] {
result.push(left[i]);
i += 1;
} else {
result.push(right[j]);
j += 1;
}
}
while i < left.len() {
result.push(left[i]);
i += 1;
}
while j < right.len() {
result.push(right[j]);
j += 1;
}
result
}Attempts:
2 left
💡 Hint
Check the loop conditions when accessing array elements by index.
✗ Incorrect
Using <= causes the code to access one index beyond the slice length, causing a runtime panic. Changing to < fixes this.
🚀 Application
advancedUsing Sorting to Find the Median
Given an unsorted list of numbers, which approach correctly finds the median using sorting?
Attempts:
2 left
💡 Hint
Median is the middle value after sorting.
✗ Incorrect
Median is the center value in sorted order. For odd length, it's the middle element. For even length, it's the average of the two middle elements.
❓ Predict Output
expertOutput of Sorting Custom Structs with PartialOrd
What is the output of this Rust code that sorts a vector of custom structs by their 'value' field?
DSA Rust
use std::cmp::Ordering; #[derive(Debug)] struct Item { name: &'static str, value: i32, } impl PartialEq for Item { fn eq(&self, other: &Self) -> bool { self.value == other.value } } impl Eq for Item {} impl PartialOrd for Item { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.value.cmp(&other.value)) } } impl Ord for Item { fn cmp(&self, other: &Self) -> Ordering { self.value.cmp(&other.value) } } fn main() { let mut items = vec![ Item { name: "apple", value: 5 }, Item { name: "banana", value: 2 }, Item { name: "cherry", value: 7 }, ]; items.sort(); for item in items { println!("{}:{}", item.name, item.value); } }
Attempts:
2 left
💡 Hint
Sorting uses the 'value' field ascending order.
✗ Incorrect
The items are sorted by value ascending: banana(2), apple(5), cherry(7). The print shows name:value per line.
