Challenge - 5 Problems
Binary Search Allocation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Minimum Pages Allocation Function
What is the output of the following JavaScript function when called with books = [10, 20, 30, 40] and students = 2?
DSA Javascript
function isPossible(books, students, mid) {
let requiredStudents = 1;
let currentSum = 0;
for (let pages of books) {
if (pages > mid) return false;
if (currentSum + pages > mid) {
requiredStudents++;
currentSum = pages;
if (requiredStudents > students) return false;
} else {
currentSum += pages;
}
}
return true;
}
function allocateMinimumPages(books, students) {
let start = 0;
let end = books.reduce((a, b) => a + b, 0);
let result = end;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (isPossible(books, students, mid)) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}
console.log(allocateMinimumPages([10, 20, 30, 40], 2));Attempts:
2 left
💡 Hint
Think about dividing the books so that the maximum pages assigned to a student is minimized.
✗ Incorrect
The minimum maximum pages a student can get is 60 by dividing books as [10, 20, 30] and [40].
🧠 Conceptual
intermediate1:30remaining
Understanding the Role of Binary Search in Allocation
Why do we use binary search in the Allocate Minimum Pages problem?
Attempts:
2 left
💡 Hint
Think about searching for a value in a range rather than searching the array itself.
✗ Incorrect
Binary search helps find the smallest maximum pages that can be assigned to any student by searching the range between the largest single book and the sum of all books.
🔧 Debug
advanced2:00remaining
Identify the Bug in Allocation Feasibility Check
What error will occur when running this code snippet for isPossible function with books = [10, 20, 30], students = 2, mid = 25?
DSA Javascript
function isPossible(books, students, mid) {
let requiredStudents = 1;
let currentSum = 0;
for (let pages of books) {
if (pages > mid) return false;
if (currentSum + pages > mid) {
requiredStudents++;
currentSum = pages;
if (requiredStudents > students) return false;
} else {
currentSum += pages;
}
}
return true;
}
console.log(isPossible([10, 20, 30], 2, 25));Attempts:
2 left
💡 Hint
Check the condition that compares pages with mid.
✗ Incorrect
The condition 'if (pages >= mid)' should be 'if (pages > mid)' because if a book has pages equal to mid, it can still be allocated.
❓ Predict Output
advanced2:00remaining
Output of Allocation with Multiple Students
What is the output of allocateMinimumPages([12, 34, 67, 90], 2)?
DSA Javascript
function isPossible(books, students, mid) {
let requiredStudents = 1;
let currentSum = 0;
for (let pages of books) {
if (pages > mid) return false;
if (currentSum + pages > mid) {
requiredStudents++;
currentSum = pages;
if (requiredStudents > students) return false;
} else {
currentSum += pages;
}
}
return true;
}
function allocateMinimumPages(books, students) {
let start = 0;
let end = books.reduce((a, b) => a + b, 0);
let result = end;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (isPossible(books, students, mid)) {
result = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
return result;
}
console.log(allocateMinimumPages([12, 34, 67, 90], 2));Attempts:
2 left
💡 Hint
Try dividing books into two groups to minimize the maximum pages.
✗ Incorrect
The minimum maximum pages is 113 by dividing as [12, 34, 67] and [90].
🧠 Conceptual
expert1:30remaining
Why is Greedy Check Used in Binary Search for Allocation?
In the Allocate Minimum Pages problem, why do we use a greedy approach inside the binary search feasibility check?
Attempts:
2 left
💡 Hint
Think about how we check if a mid value is valid during binary search.
✗ Incorrect
The greedy check tries to allocate books to students without exceeding the mid value to verify feasibility.