Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the maximum number of pages in the books array.
DSA Go
maxPages := 0 for _, pages := range books { if pages > [1] { maxPages = pages } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing pages with 0 instead of maxPages.
Using the length of books instead of pages.
✗ Incorrect
We compare each book's pages with the current maxPages to find the maximum pages.
2fill in blank
mediumComplete the code to check if allocation of books is possible with given maxPages.
DSA Go
func isPossible(books []int, students int, maxPages int) bool {
requiredStudents := 1
currentSum := 0
for _, pages := range books {
if currentSum + pages > [1] {
requiredStudents++
currentSum = pages
} else {
currentSum += pages
}
}
return requiredStudents <= students
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with students instead of maxPages.
Using currentSum incorrectly in the condition.
✗ Incorrect
We check if adding pages exceeds maxPages to allocate books to next student.
3fill in blank
hardFix the error in the binary search loop condition to find minimum pages.
DSA Go
low := max(books) high := sum(books) result := high for low <= [1] { mid := (low + high) / 2 if isPossible(books, students, mid) { result = mid high = mid - 1 } else { low = mid + 1 } } return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low <= low which causes infinite loop.
Using mid in loop condition which is incorrect.
✗ Incorrect
The loop should continue while low is less than or equal to high.
4fill in blank
hardFill both blanks to correctly calculate the sum of pages and maximum pages in the books slice.
DSA Go
sumPages := 0 maxPages := 0 for _, pages := range books { sumPages += [1] if pages > [2] { maxPages = pages } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding maxPages instead of pages to sumPages.
Comparing pages with sumPages instead of maxPages.
✗ Incorrect
Add pages to sumPages and compare pages with maxPages to find maximum.
5fill in blank
hardFill all three blanks to implement the binary search for minimum pages allocation.
DSA Go
low := [1] high := [2] result := high for low <= high { mid := (low + high) / 2 if [3](books, students, mid) { result = mid high = mid - 1 } else { low = mid + 1 } } return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to 0 instead of maxPages.
Using wrong function name instead of isPossible.
Setting high to maxPages instead of sumPages.
✗ Incorrect
Set low to maxPages, high to sumPages, and use isPossible to check feasibility.