Challenge - 5 Problems
Binary Search on Answer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
Think about how the array can be split into 2 subarrays with minimum largest sum.
✗ Incorrect
The function tries to find the smallest maximum subarray sum possible when splitting the array into m parts. For [7,2,5,10,8] and m=2, the minimum largest sum is 18, achieved by splitting as [7,2,5] and [10,8].
🧠 Conceptual
intermediate1:30remaining
Understanding Binary Search on Answer Technique
Which of the following best describes the main idea behind the binary search on answer technique?
Attempts:
2 left
💡 Hint
Think about searching over a range of values, not array indices.
✗ Incorrect
Binary search on answer searches over a range of possible answers (not array indices) to find the smallest or largest value that meets a problem's condition.
🔧 Debug
advanced2: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));Attempts:
2 left
💡 Hint
Check the condition that triggers a new worker assignment.
✗ Incorrect
Using '>=' causes a new worker to be assigned even when currentTime + task equals maxTime, which is unnecessary and leads to incorrect results.
❓ Predict Output
advanced2: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));Attempts:
2 left
💡 Hint
Think about placing cows with maximum minimum distance between them.
✗ Incorrect
The maximum minimum distance to place 3 cows in stalls [1,2,4,8,9] is 3, placing cows at positions 1, 4, and 8.
🧠 Conceptual
expert1:30remaining
When to Use Binary Search on Answer Technique?
Which scenario is the best fit for applying binary search on answer technique?
Attempts:
2 left
💡 Hint
Think about searching over a range of possible answers with a feasibility check.
✗ Incorrect
Binary search on answer is used when the answer is a number in a range and you can check if a candidate answer satisfies the problem's constraints.