Challenge - 5 Problems
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of maximum minimum distance calculation
What is the output of the Rust code that calculates the maximum minimum distance for placing 3 cows in stalls at positions [1, 2, 8, 4, 9]?
DSA Rust
fn can_place_cows(stalls: &Vec<i32>, cows: i32, dist: i32) -> bool { let mut count = 1; let mut last_position = stalls[0]; for &stall in stalls.iter().skip(1) { if stall - last_position >= dist { count += 1; last_position = stall; if count == cows { return true; } } } false } fn aggressive_cows(stalls: &mut Vec<i32>, cows: i32) -> i32 { stalls.sort(); let mut low = 0; let mut high = stalls[stalls.len() - 1] - stalls[0]; let mut result = 0; while low <= high { let mid = (low + high) / 2; if can_place_cows(stalls, cows, mid) { result = mid; low = mid + 1; } else { high = mid - 1; } } result } fn main() { let mut stalls = vec![1, 2, 8, 4, 9]; let cows = 3; let max_min_dist = aggressive_cows(&mut stalls, cows); println!("{}", max_min_dist); }
Attempts:
2 left
💡 Hint
Think about placing cows with the largest minimum gap possible between them.
✗ Incorrect
The code sorts the stalls and uses binary search to find the largest minimum distance. For stalls [1,2,4,8,9] and 3 cows, the maximum minimum distance is 3.
🧠 Conceptual
intermediateUnderstanding the binary search range in Aggressive Cows problem
In the Aggressive Cows problem, what is the correct initial range for binary searching the minimum distance between cows?
Attempts:
2 left
💡 Hint
Consider the smallest and largest possible distances between cows.
✗ Incorrect
The minimum distance can be as low as 0 and as high as the distance between the farthest stalls.
🔧 Debug
advancedIdentify the bug causing incorrect placement check
Given the following Rust function for checking if cows can be placed with at least 'dist' distance, which option correctly identifies the bug?
DSA Rust
fn can_place_cows(stalls: &Vec<i32>, cows: i32, dist: i32) -> bool { let mut count = 1; let mut last_position = stalls[0]; for &stall in stalls.iter().skip(1) { if stall - last_position > dist { count += 1; last_position = stall; if count == cows { return true; } } } false }
Attempts:
2 left
💡 Hint
Check the comparison operator for distance condition.
✗ Incorrect
Using '>' excludes the case where distance equals 'dist', which is valid placement.
📝 Syntax
advancedIdentify the syntax error in Rust code snippet
What is the syntax error in this Rust code snippet for binary searching the maximum minimum distance?
DSA Rust
while low <= high { let mid = (low + high) / 2 if can_place_cows(stalls, cows, mid) { result = mid; low = mid + 1; } else { high = mid - 1; } }
Attempts:
2 left
💡 Hint
Check line endings in Rust.
✗ Incorrect
Rust requires semicolons after statements like variable declarations.
🚀 Application
expertMaximum minimum distance for large input stalls
Given 5 stalls at positions [10, 20, 30, 40, 50] and 3 cows, what is the maximum minimum distance to place all cows?
Attempts:
2 left
💡 Hint
Try placing cows with equal spacing and check feasibility.
✗ Incorrect
Placing cows at 10, 30, and 50 gives minimum distance 20. The maximum minimum distance is 20, and checking feasibility confirms it works.
