0
0
DSA Javascriptprogramming~20 mins

Allocate Minimum Pages Binary Search on Answer in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Allocation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A90
B70
C60
D100
Attempts:
2 left
💡 Hint
Think about dividing the books so that the maximum pages assigned to a student is minimized.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of Binary Search in Allocation
Why do we use binary search in the Allocate Minimum Pages problem?
ATo find the minimum possible maximum pages assigned to a student efficiently
BTo find the minimum number of students needed to allocate books
CTo sort the books array before allocation
DTo find the maximum number of pages in a single book
Attempts:
2 left
💡 Hint
Think about searching for a value in a range rather than searching the array itself.
🔧 Debug
advanced
2: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));
AInfinite loop occurs
BReturns true correctly
CThrows a ReferenceError due to undefined variable
DReturns false incorrectly because of wrong comparison operator
Attempts:
2 left
💡 Hint
Check the condition that compares pages with mid.
Predict Output
advanced
2: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));
A124
B113
C157
D90
Attempts:
2 left
💡 Hint
Try dividing books into two groups to minimize the maximum pages.
🧠 Conceptual
expert
1: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?
ATo verify if a given maximum page limit can be assigned without exceeding student count
BTo quickly find the exact minimum pages without searching
CTo sort the books in ascending order before allocation
DTo calculate the total sum of pages for all books
Attempts:
2 left
💡 Hint
Think about how we check if a mid value is valid during binary search.