0
0
DSA Typescriptprogramming~20 mins

Binary Search on Answer Technique in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search on Answer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search on Answer for Minimum Maximum Subarray Sum
What is the output of the following TypeScript code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA Typescript
function canSplit(nums: number[], maxSum: number, m: number): boolean {
  let currentSum = 0, count = 1;
  for (const num of nums) {
    if (num > maxSum) return false;
    if (currentSum + num > maxSum) {
      count++;
      currentSum = num;
      if (count > m) return false;
    } else {
      currentSum += num;
    }
  }
  return true;
}

function splitArray(nums: number[], m: number): number {
  let left = Math.max(...nums);
  let right = nums.reduce((a, b) => a + b, 0);
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (canSplit(nums, mid, m)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const nums = [7,2,5,10,8];
const m = 2;
console.log(splitArray(nums, m));
A18
B15
C20
D23
Attempts:
2 left
💡 Hint
Think about how the array can be split into 2 subarrays with minimum largest sum.
🧠 Conceptual
intermediate
1:30remaining
Understanding Binary Search on Answer Technique
Which of the following best describes the main idea behind the binary search on answer technique?
AUsing binary search to find the correct value in a sorted array directly.
BUsing binary search to find the index of a target value in a linked list.
CUsing binary search to sort an unsorted array efficiently.
DUsing binary search over a range of possible answers to find the minimum or maximum value that satisfies a condition.
Attempts:
2 left
💡 Hint
Think about searching over a range of values, not array indices.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Binary Search on Answer Implementation
What is the bug in the following TypeScript code that tries to find the minimum maximum time to complete tasks by workers using binary search on answer?
DSA Typescript
function canComplete(tasks: number[], maxTime: number, workers: number): boolean {
  let currentTime = 0;
  let count = 1;
  for (const task of tasks) {
    if (task > maxTime) return false;
    if (currentTime + task > maxTime) {
      count++;
      currentTime = task;
      if (count > workers) return false;
    } else {
      currentTime += task;
    }
  }
  return true;
}

function minTime(tasks: number[], workers: number): number {
  let left = Math.max(...tasks);
  let right = tasks.reduce((a, b) => a + b, 0);
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (canComplete(tasks, mid, workers)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const tasks = [3, 6, 7, 11];
const workers = 2;
console.log(minTime(tasks, workers));
AThe mid calculation should use (left + right + 1) / 2 to avoid infinite loop.
BThe condition 'currentTime + task >= maxTime' should be 'currentTime + task > maxTime' to avoid extra worker count.
CThe function should return false if tasks array is empty.
DThe initial left boundary should be 0 instead of max task time.
Attempts:
2 left
💡 Hint
Check the condition that triggers a new worker assignment.
Predict Output
advanced
2:00remaining
Output of Binary Search on Answer for Aggressive Cows Problem
What is the output of the following TypeScript code that uses binary search on answer to place cows in stalls maximizing minimum distance?
DSA Typescript
function canPlaceCows(stalls: number[], distance: number, cows: number): boolean {
  let count = 1;
  let lastPosition = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPosition >= distance) {
      count++;
      lastPosition = stalls[i];
      if (count >= cows) return true;
    }
  }
  return false;
}

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

const stalls = [1, 2, 8, 4, 9];
const cows = 3;
console.log(aggressiveCows(stalls, cows));
A4
B5
C3
D6
Attempts:
2 left
💡 Hint
Think about placing cows with maximum minimum distance between them.
🧠 Conceptual
expert
1:30remaining
When to Use Binary Search on Answer Technique?
Which scenario is the best fit for applying binary search on answer technique?
AWhen the problem asks for an optimal numeric value that can be checked by a yes/no condition over a range.
BWhen you need to find an element in a sorted array by index.
CWhen you want to sort an array in O(n log n) time.
DWhen you need to traverse a tree in depth-first order.
Attempts:
2 left
💡 Hint
Think about searching over a range of possible answers with a feasibility check.