Challenge - 5 Problems
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of maximum minimum distance calculation
What is the output of the following Go code that calculates the maximum minimum distance to place 3 cows in given stalls?
DSA Go
package main import ( "fmt" "sort" ) func canPlaceCows(stalls []int, cows int, dist int) bool { count := 1 lastPos := stalls[0] for i := 1; i < len(stalls); i++ { if stalls[i]-lastPos >= dist { count++ lastPos = stalls[i] if count == cows { return true } } } return false } func maxMinDistance(stalls []int, cows int) int { sort.Ints(stalls) low, high := 0, stalls[len(stalls)-1]-stalls[0] result := 0 for low <= high { mid := (low + high) / 2 if canPlaceCows(stalls, cows, mid) { result = mid low = mid + 1 } else { high = mid - 1 } } return result } func main() { stalls := []int{1, 2, 8, 4, 9} cows := 3 fmt.Println(maxMinDistance(stalls, cows)) }
Attempts:
2 left
💡 Hint
Think about placing cows with the largest minimum gap possible using binary search.
✗ Incorrect
The maximum minimum distance to place 3 cows in stalls at positions [1,2,4,8,9] is 3. The cows can be placed at positions 1, 4, and 8.
🧠 Conceptual
intermediate1:30remaining
Understanding the role of sorting in Aggressive Cows problem
Why is it necessary to sort the stall positions before applying the binary search approach in the Aggressive Cows problem?
Attempts:
2 left
💡 Hint
Think about how distances between stalls are calculated.
✗ Incorrect
Sorting allows us to check distances between stalls in order, which is essential for placing cows with minimum distance constraints.
🔧 Debug
advanced2:00remaining
Identify the bug causing incorrect output
The following Go code attempts to solve the Aggressive Cows problem but returns 2 instead of the correct answer 3. What is the bug?
DSA Go
func canPlaceCows(stalls []int, cows int, dist int) bool { count := 1 lastPos := stalls[0] for i := 1; i < len(stalls); i++ { if stalls[i]-lastPos > dist { count++ lastPos = stalls[i] if count == cows { return true } } } return false }
Attempts:
2 left
💡 Hint
Check the condition that decides if a cow can be placed at a stall.
✗ Incorrect
Using > dist excludes cases where the distance equals dist, which is valid placement. Changing to >= fixes the bug.
❓ Predict Output
advanced2:00remaining
Output of maximum minimum distance with larger input
What is the output of the following Go code that calculates the maximum minimum distance to place 4 cows in given stalls?
DSA Go
package main import ( "fmt" "sort" ) func canPlaceCows(stalls []int, cows int, dist int) bool { count := 1 lastPos := stalls[0] for i := 1; i < len(stalls); i++ { if stalls[i]-lastPos >= dist { count++ lastPos = stalls[i] if count == cows { return true } } } return false } func maxMinDistance(stalls []int, cows int) int { sort.Ints(stalls) low, high := 0, stalls[len(stalls)-1]-stalls[0] result := 0 for low <= high { mid := (low + high) / 2 if canPlaceCows(stalls, cows, mid) { result = mid low = mid + 1 } else { high = mid - 1 } } return result } func main() { stalls := []int{1, 2, 4, 8, 9, 12, 16} cows := 4 fmt.Println(maxMinDistance(stalls, cows)) }
Attempts:
2 left
💡 Hint
Try placing cows with increasing minimum distances and check feasibility.
✗ Incorrect
The largest minimum distance to place 4 cows in stalls [1,2,4,8,9,12,16] is 4. One placement is at 1, 8, 12, and 16.
🧠 Conceptual
expert2:30remaining
Why binary search is suitable for Aggressive Cows problem?
Why is binary search an efficient approach to find the maximum minimum distance in the Aggressive Cows problem?
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the search space using a yes/no check.
✗ Incorrect
Binary search efficiently narrows down the maximum minimum distance by checking if cows can be placed with a given distance, which is a yes/no decision problem.