0
0
DSA Javascriptprogramming~10 mins

Allocate Minimum Pages Binary Search on Answer in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if allocation is possible with given max pages.

DSA Javascript
function isPossible(pages, students, maxPages) {
  let requiredStudents = 1;
  let currentSum = 0;
  for (let i = 0; i < pages.length; i++) {
    if (pages[i] > maxPages) return false;
    if (currentSum + pages[i] > [1]) {
      requiredStudents++;
      currentSum = pages[i];
      if (requiredStudents > students) return false;
    } else {
      currentSum += pages[i];
    }
  }
  return true;
}
Drag options to blanks, or click blank then click option'
AmaxPages
Bstudents
Cpages.length
DcurrentSum
Attempts:
3 left
💡 Hint
Common Mistakes
Using students instead of maxPages in the condition.
Comparing with pages.length instead of maxPages.
2fill in blank
medium

Complete the code to find the minimum pages using binary search.

DSA Javascript
function findPages(pages, students) {
  let start = Math.max(...pages);
  let end = pages.reduce((a, b) => a + b, 0);
  let result = end;
  while (start <= end) {
    let mid = Math.floor((start + end) / 2);
    if (isPossible(pages, students, [1])) {
      result = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Astart
Bmid
Cend
Dstudents
Attempts:
3 left
💡 Hint
Common Mistakes
Using start or end directly instead of mid.
Passing students as maxPages parameter.
3fill in blank
hard

Fix the error in the loop condition to avoid infinite loop.

DSA Javascript
while (start [1] end) {
  let mid = Math.floor((start + end) / 2);
  if (isPossible(pages, students, mid)) {
    result = mid;
    end = mid - 1;
  } else {
    start = mid + 1;
  }
}
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes missing the last check.
Using > or >= reverses the logic causing no loop.
4fill in blank
hard

Fill both blanks to correctly calculate the sum of pages.

DSA Javascript
let totalPages = pages[1]((a, b) => a [2] b, 0);
Drag options to blanks, or click blank then click option'
A.reduce
B+
C-
D.map
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of reduce.
Using subtraction instead of addition.
5fill in blank
hard

Fill all three blanks to correctly find the max pages in the array.

DSA Javascript
let maxPage = pages[1]((max, current) => {
  return current [2] max ? current : max;
}, [3]);
Drag options to blanks, or click blank then click option'
A.reduce
B>
C0
D-Infinity
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as initial max can fail if all pages are negative.
Using < instead of > reverses max logic.