Challenge - 5 Problems
Binary Search on Answer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search on Answer for Minimum Maximum Subarray Sum
What is the output of the following code that uses binary search on answer to find the minimum largest sum among 2 subarrays?
DSA Python
def can_split(nums, max_sum, m): current_sum = 0 count = 1 for num in nums: if current_sum + num > max_sum: count += 1 current_sum = num if count > m: return False else: current_sum += num return True def split_array(nums, m): left, right = max(nums), sum(nums) while left < right: mid = (left + right) // 2 if can_split(nums, mid, m): right = mid else: left = mid + 1 return left nums = [7,2,5,10,8] m = 2 print(split_array(nums, m))
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the maximum allowed subarray sum.
✗ Incorrect
The code finds the smallest maximum sum possible when splitting the array into 2 subarrays. The answer is 18 because splitting as [7,2,5], [10,8] gives max sums 14 and 18.
🧠 Conceptual
intermediateUnderstanding 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 how binary search is used on a range of possible answers, not on the input data directly.
✗ Incorrect
Binary search on answer works by guessing an answer in a range and checking if it is possible (feasible). Based on feasibility, it narrows the range until the correct answer is found.
🔧 Debug
advancedFind the error in binary search on answer implementation
What error does the following code raise when trying to find the minimum maximum load for workers?
DSA Python
def can_distribute(weights, max_load, workers): current_load = 0 count = 1 for w in weights: if w > max_load: return False if current_load + w <= max_load: current_load += w else: count += 1 current_load = w return count <= workers def min_load(weights, workers): left, right = 0, sum(weights) while left < right: mid = (left + right) // 2 if can_distribute(weights, mid, workers): right = mid else: left = mid + 1 return left weights = [10, 20, 30, 40] workers = 2 print(min_load(weights, workers))
Attempts:
2 left
💡 Hint
Check the initial values of left and right and how mid is calculated.
✗ Incorrect
The code correctly implements binary search on answer. The minimal maximum load is 60 by splitting as [10,20,30] and [40]. No error occurs.
❓ Predict Output
advancedOutput of Binary Search on Answer for Minimum Time to Complete Jobs
What is the output of this code that finds the minimum time to finish all jobs by k workers using binary search on answer?
DSA Python
def can_finish(jobs, time_limit, k): workers = 1 current_time = 0 for job in jobs: if job > time_limit: return False if current_time + job <= time_limit: current_time += job else: workers += 1 current_time = job if workers > k: return False return True def min_time(jobs, k): left, right = max(jobs), sum(jobs) while left < right: mid = (left + right) // 2 if can_finish(jobs, mid, k): right = mid else: left = mid + 1 return left jobs = [3, 6, 8, 11] k = 2 print(min_time(jobs, k))
Attempts:
2 left
💡 Hint
Consider how jobs are assigned to workers to keep total time under mid.
✗ Incorrect
The minimal time is 17 by splitting jobs as [3,6,8] and [11].
🚀 Application
expertUsing Binary Search on Answer to Find Maximum Minimum Distance
You want to place 3 routers in houses located at positions [1, 2, 8, 4, 9]. Using binary search on answer, what is the maximum minimum distance between any two routers?
Attempts:
2 left
💡 Hint
Sort the house positions and check feasibility of placing routers with given minimum distance.
✗ Incorrect
Sorted positions: [1,2,4,8,9]. Maximum minimum distance is 3 by placing routers at positions 1, 4, and 8.
