Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Aggressive Cows Maximum Minimum Distance in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of placing cows with minimum distance 3
Given the stalls at positions [1, 2, 4, 8, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA Typescript
function canPlaceCows(stalls: number[], cows: number, dist: number): boolean {
  let count = 1;
  let lastPos = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPos >= dist) {
      count++;
      lastPos = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

function aggressiveCows(stalls: number[], cows: number): number {
  stalls.sort((a, b) => a - b);
  let low = 1;
  let high = stalls[stalls.length - 1] - stalls[0];
  let result = 0;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (canPlaceCows(stalls, cows, mid)) {
      result = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return result;
}

console.log(aggressiveCows([1, 2, 4, 8, 9], 3));
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Try placing cows starting with the smallest distance and increase it using binary search.
🧠 Conceptual
intermediate
1:30remaining
Why do we sort stalls before placing cows?
In the Aggressive Cows problem, why is it necessary to sort the stall positions before applying the binary search approach?
ATo reduce the number of stalls to consider by removing duplicates.
BSorting is not necessary; cows can be placed in any order.
CTo ensure we can check distances in increasing order and place cows greedily.
DTo make the stalls positions negative for easier calculation.
Attempts:
2 left
💡 Hint
Think about how distance checking works between stalls.
🔧 Debug
advanced
1:30remaining
Identify the error in this code snippet
What error will this TypeScript code produce when trying to find the maximum minimum distance for aggressive cows? function canPlaceCows(stalls: number[], cows: number, dist: number): boolean { let count = 1; let lastPos = stalls[0]; for (let i = 1; i < stalls.length; i++) { if (stalls[i] - lastPos > dist) { count++; lastPos = stalls[i]; if (count === cows) return true; } } return false; } // rest of code omitted for brevity
AThe condition 'stalls[i] - lastPos > dist' causes incorrect placement, missing valid positions.
BSyntaxError due to missing semicolon after 'let count = 1'
CTypeError because 'stalls' is not defined
DNo error, code runs correctly
Attempts:
2 left
💡 Hint
Check the comparison operator in the if condition.
🚀 Application
advanced
1:30remaining
Maximum minimum distance for large input
Given 5 stalls at positions [10, 20, 30, 40, 50] and 3 cows, what is the maximum minimum distance to place all cows?
A20
B10
C15
D25
Attempts:
2 left
💡 Hint
Try placing cows with distances 10, 15, 20 and check feasibility.
🧠 Conceptual
expert
2:00remaining
Why binary search works on distance, not positions?
In the Aggressive Cows problem, why do we apply binary search on the minimum distance between cows instead of the stall positions themselves?
ABecause searching positions directly would require checking all permutations of cow placements.
BBecause stall positions are unsorted and cannot be searched.
CBecause binary search only works on numeric arrays, not positions.
DBecause the answer is a distance value, and binary searching distances narrows down the feasible minimum gap efficiently.
Attempts:
2 left
💡 Hint
Think about what the problem is asking to maximize and how binary search helps.

Practice

(1/5)
1. What is the main goal of the Aggressive Cows Maximum Minimum Distance problem?
easy
A. To place cows in stalls so that the minimum distance between any two cows is as large as possible
B. To find the maximum number of cows that can fit in a single stall
C. To minimize the total distance cows travel between stalls
D. To arrange cows so that all are in consecutive stalls

Solution

  1. Step 1: Understand the problem objective

    The problem asks to place cows in given stalls such that the smallest distance between any two cows is as large as possible.
  2. Step 2: Identify the correct goal

    This means maximizing the minimum distance between cows, not minimizing travel or fitting all cows in one stall.
  3. Final Answer:

    To place cows in stalls so that the minimum distance between any two cows is as large as possible -> Option A
  4. Quick Check:

    Maximize minimum distance = To place cows in stalls so that the minimum distance between any two cows is as large as possible [OK]
Hint: Focus on maximizing the smallest gap between cows [OK]
Common Mistakes:
  • Thinking it's about total distance traveled
  • Assuming cows must be in consecutive stalls
  • Confusing maximum distance with maximum number of cows
2. Which of the following TypeScript function signatures correctly represents the binary search approach used in the Aggressive Cows problem?
easy
A. function maxDistance(cows: number[], stalls: number): number
B. function placeCows(stalls: string[], cows: number): number
C. function canPlaceCows(stalls: number[], cows: number, distance: number): boolean
D. function binarySearch(distance: number[], cows: number): boolean

Solution

  1. Step 1: Identify correct parameter types and purpose

    The function should check if cows can be placed in stalls with at least a given distance, so it needs stalls array, number of cows, and distance.
  2. Step 2: Match signature to this logic

    function canPlaceCows(stalls: number[], cows: number, distance: number): boolean matches this: stalls as number array, cows count, and distance as number, returning boolean.
  3. Final Answer:

    function canPlaceCows(stalls: number[], cows: number, distance: number): boolean -> Option C
  4. Quick Check:

    Correct parameters and return type = function canPlaceCows(stalls: number[], cows: number, distance: number): boolean [OK]
Hint: Check parameter types match problem data (numbers, arrays) [OK]
Common Mistakes:
  • Using wrong parameter types like string[] for stalls
  • Returning wrong data type (e.g., number instead of boolean)
  • Mixing order of parameters
3. Given stalls at positions [1, 2, 8, 4, 9] and 3 cows, what is the maximum minimum distance between any two cows?
medium
A. 2
B. 4
C. 5
D. 3

Solution

  1. Step 1: Sort stall positions

    Sorted stalls: [1, 2, 4, 8, 9]
  2. Step 2: Use binary search to find max minimum distance

    Try distance 3: place first cow at 1, second at 4 (distance 3), third at 8 (distance 4). Works.
  3. Step 3: Check if distance 4 works

    Try distance 4: place first at 1, second at 8 (distance 7), third must be at least 4 away from 8, but 9 is only 1 away. Fails.
  4. Final Answer:

    3 -> Option D
  5. Quick Check:

    Max min distance = 3 [OK]
Hint: Sort stalls first, then binary search distances [OK]
Common Mistakes:
  • Not sorting stalls before calculation
  • Testing distances incorrectly
  • Assuming max distance equals max stall difference
4. Consider this buggy TypeScript snippet for checking if cows can be placed:
function canPlaceCows(stalls: number[], cows: number, dist: number): boolean {
  let count = 1;
  let lastPos = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPos >= dist) {
      count++;
      lastPos = stalls[i];
    }
  }
  return count >= cows;
}

What is the main bug if stalls array is not sorted?
medium
A. The function will throw a runtime error due to index out of bounds
B. The function may incorrectly count placements because stalls are unsorted
C. The function returns a number instead of boolean
D. The function does not increment count properly

Solution

  1. Step 1: Understand the function logic

    The function counts how many cows can be placed with at least 'dist' apart, assuming stalls are sorted.
  2. Step 2: Identify effect of unsorted stalls

    If stalls are unsorted, the distance check stalls[i] - lastPos may be incorrect, causing wrong count of cows placed.
  3. Final Answer:

    The function may incorrectly count placements because stalls are unsorted -> Option B
  4. Quick Check:

    Unsorted stalls cause wrong counting = The function may incorrectly count placements because stalls are unsorted [OK]
Hint: Always sort stalls before placement checks [OK]
Common Mistakes:
  • Ignoring sorting requirement
  • Assuming function throws error on unsorted input
  • Confusing return type or count increment logic
5. You have stalls at positions [1, 8, 15, 22, 25] and 4 cows. Using the binary search method, what is the maximum minimum distance you can place the cows apart?
hard
A. 7
B. 5
C. 8
D. 6

Solution

  1. Step 1: Sort stalls (already sorted)

    Stalls: [1, 8, 15, 22, 25]
  2. Step 2: Apply binary search on distance range

    Minimum distance can be 1, max distance is 25-1=24. Check mid distances:
  3. Step 3: Check distance 9

    Place first cow at 1, next at 15 (distance 14), third at 25 (distance 10). Only 3 cows placed, need 4, so 9 fails.
  4. Step 4: Check distance 7

    Place cows at 1, 8, 15, 22. Distances between placed cows are at least 7. 4 cows placed successfully.
  5. Step 5: Check distance 8

    Place cows at 1, 15, 25. Only 3 cows placed, 4 needed, so 8 fails.
  6. Final Answer:

    7 -> Option A
  7. Quick Check:

    Max min distance = 7 [OK]
Hint: Test mid distances with binary search, adjust range accordingly [OK]
Common Mistakes:
  • Not testing all mid values carefully
  • Assuming max distance equals max stall difference
  • Placing cows without checking minimum distance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard