0
0
DSA Typescriptprogramming~10 mins

Aggressive Cows Maximum Minimum Distance in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the stalls array.

DSA Typescript
stalls.sort((a, b) => a [1] b);
Drag options to blanks, or click blank then click option'
A+
B>
C<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '-' causes descending order.
Using '-' directly without a function returns NaN.
2fill in blank
medium

Complete the code to check if cows can be placed with at least mid distance.

DSA Typescript
if (stalls[i] - lastPos [1] mid) {
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' places cows too close.
Using '==' is too strict and misses valid placements.
3fill in blank
hard

Fix the error in the binary search loop condition.

DSA Typescript
while (low [1] high) {
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses checking when low equals high.
Using '>' or '>=' reverses the logic.
4fill in blank
hard

Fill both blanks to update the binary search bounds correctly.

DSA Typescript
if (canPlaceCows(stalls, cows, mid)) {
  result = mid;
  low = mid [1] 1;
} else {
  high = mid [2] 1;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '+' and '-' causes infinite loops.
Using '*' or '/' is incorrect for adjusting bounds.
5fill in blank
hard

Fill the blanks to complete the canPlaceCows function logic.

DSA Typescript
function canPlaceCows(stalls: number[], cows: number, mid: number): boolean {
  let count = 1;
  let lastPos = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPos [1] mid) {
      count++;
      lastPos = stalls[i];
      if (count [2] cows) {
        return true;
      }
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
A>=
B==
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' for distance check is too strict.
Using '<=' for count comparison is incorrect.