Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if allocation is possible with given max pages.
DSA Python
def is_possible(arr, n, m, [1]): students = 1 pages_sum = 0 for i in range(n): if arr[i] > max_pages: return False if pages_sum + arr[i] > max_pages: students += 1 pages_sum = arr[i] if students > m: return False else: pages_sum += arr[i] return True
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined in the function parameters.
Confusing max_pages with mid or min_pages.
✗ Incorrect
The function checks if allocation is possible with the given maximum pages allowed per student, so the parameter should be max_pages.
2fill in blank
mediumComplete the code to calculate mid value for binary search.
DSA Python
def find_pages(arr, n, m): start = max(arr) end = sum(arr) result = end while start <= end: [1] = (start + end) // 2 if is_possible(arr, n, m, mid): result = mid end = mid - 1 else: start = mid + 1 return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names not matching the rest of the code.
Using float division instead of integer division.
✗ Incorrect
The variable mid is commonly used to represent the middle value in binary search calculations.
3fill in blank
hardFix the error in the loop condition to correctly perform binary search.
DSA Python
def find_pages(arr, n, m): start = max(arr) end = sum(arr) result = end while [1]: mid = (start + end) // 2 if is_possible(arr, n, m, mid): result = mid end = mid - 1 else: start = mid + 1 return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causing missing the last check.
Using >= or == which breaks the loop logic.
✗ Incorrect
The binary search loop should continue while start is less than or equal to end to cover all possible values.
4fill in blank
hardFill both blanks to correctly update the binary search range after checking feasibility.
DSA Python
def find_pages(arr, n, m): start = max(arr) end = sum(arr) result = end while start <= end: mid = (start + end) // 2 if is_possible(arr, n, m, mid): result = mid [1] = mid - 1 else: [2] = mid + 1 return result
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping start and end updates.
Updating result instead of start or end.
✗ Incorrect
When allocation is possible, we try to reduce the upper bound by setting end = mid - 1. Otherwise, we increase the lower bound by setting start = mid + 1.
5fill in blank
hardFill all three blanks to complete the function that returns minimum pages allocation.
DSA Python
def allocate_min_pages(arr, n, m): if m > n: return -1 start = max(arr) end = sum(arr) result = end while start <= end: mid = (start + end) // 2 if is_possible(arr, n, m, mid): result = [1] end = [2] else: start = [3] return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for start or end updates.
Not updating result correctly.
✗ Incorrect
When allocation is possible, update result to mid and move end to mid - 1. Otherwise, move start to mid + 1.
