Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the low boundary for binary search as the maximum pages in any single book.
DSA C#
int low = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to 0 or sum of all pages instead of max pages.
Using Min() instead of Max() for low boundary.
✗ Incorrect
The minimum number of pages allocated to a student cannot be less than the maximum pages in a single book.
2fill in blank
mediumComplete the code to initialize the high boundary for binary search as the sum of all pages in the books.
DSA C#
int high = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to max pages instead of sum.
Using Min() or 0 for high boundary.
✗ Incorrect
The maximum number of pages allocated to a student can be the sum of all pages if one student reads all books.
3fill in blank
hardFix the error in the condition to check if the current mid value can be a valid maximum page allocation.
DSA C#
if (IsPossible(books, students, [1]))
Attempts:
3 left
💡 Hint
Common Mistakes
Using low or high instead of mid in the feasibility check.
Using sum which is not defined in this context.
✗ Incorrect
We check feasibility using the mid value as the candidate maximum pages.
4fill in blank
hardFill both blanks to update the binary search boundaries correctly after checking feasibility.
DSA C#
if (IsPossible(books, students, mid)) { result = mid; [1] = mid [2] 1; } else { low = mid + 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Updating low instead of high when mid is possible.
Using '+' instead of '-' in the update.
✗ Incorrect
If mid is possible, we try to find a smaller maximum by moving high down to mid - 1.
5fill in blank
hardFill all three blanks to complete the IsPossible function that checks if books can be allocated within maxPages to given students.
DSA C#
bool IsPossible(int[] books, int students, int maxPages) {
int count = 1;
int currentSum = 0;
foreach (int pages in books) {
if (pages > maxPages) return false;
if (currentSum + pages > [1]) {
count++;
currentSum = pages;
if (count > [2]) return false;
} else {
currentSum += [3];
}
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables in comparisons or additions.
Not resetting currentSum correctly after incrementing count.
✗ Incorrect
We check if adding pages exceeds maxPages, increment student count, and add pages to currentSum otherwise.
