Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using students instead of maxPages in the condition.
Comparing with pages.length instead of maxPages.
✗ Incorrect
We check if adding the current book pages exceeds the maxPages limit allowed per student.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start or end directly instead of mid.
Passing students as maxPages parameter.
✗ Incorrect
We check feasibility with mid as the max pages limit to narrow down the search.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes missing the last check.
Using > or >= reverses the logic causing no loop.
✗ Incorrect
Using <= ensures the loop runs until start passes end, covering all possibilities.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of reduce.
Using subtraction instead of addition.
✗ Incorrect
We use reduce to sum all pages by adding each element to accumulator.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as initial max can fail if all pages are negative.
Using < instead of > reverses max logic.
✗ Incorrect
Reduce finds max by comparing current with max; initial max is -Infinity to cover all values.