0
0
DSA Goprogramming~10 mins

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

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

Complete 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'
A0
Bpages
CmaxPages
Dlen(books)
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing pages with 0 instead of maxPages.
Using the length of books instead of pages.
2fill in blank
medium

Complete 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'
Astudents
BmaxPages
Clen(books)
DcurrentSum
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with students instead of maxPages.
Using currentSum incorrectly in the condition.
3fill in blank
hard

Fix 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'
Amid
Blow
Cresult
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Using low <= low which causes infinite loop.
Using mid in loop condition which is incorrect.
4fill in blank
hard

Fill 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'
Apages
BsumPages
CmaxPages
Dlen(books)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding maxPages instead of pages to sumPages.
Comparing pages with sumPages instead of maxPages.
5fill in blank
hard

Fill 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'
AmaxPages
BsumPages
CisPossible
Dlen(books)
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.