DSA Tracker

Hard

Book Allocation Problem

A hard Binary Search problem included in Apna College, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Search
Sheets
2
Core for
7 roles
Platform
GeeksforGeeks

The problem

There are n books arranged in order, each with a given number of pages. You must allocate all books to k students such that each student gets a contiguous set of books. Find the minimum possible value of the maximum number of pages assigned to any student.

Example 1

Input
pages = [12,34,67,90], k = 2
Output
113
Why
Allocate [12,34,67] to student 1 (113 pages) and [90] to student 2 (90 pages). Maximum is 113.

Example 2

Input
pages = [5,17,100,11], k = 4
Output
100
Why
Each student gets exactly one book. The maximum pages assigned is 100.

Constraints

  • 1 <= k <= pages.length <= 10^3
  • 1 <= pages[i] <= 10^6

How to think about it

Updated 2026-09-09

Allocating contiguous items to minimize the maximum student workload exhibits a monotonic feasibility property: higher page allowances require fewer students. Binary searching the page ceiling turns an optimization problem into an efficient sequence of greedy allocation checks.

Approaches, worst first

  1. Recursive partitioning with memoization

    time O(k * n^2) · space O(k * n)

    Try all contiguous partition points for each student recursively and memoize results. Accurate and directly models all allocations, but explores multiple partition boundaries that binary search eliminates directly.

  2. Binary search on student page ceilingWrite this one

    time O(n log(sum(pages))) · space O(1)

    Search candidate page limits between max(pages) and sum(pages). For each limit, greedily sum pages for a student until the next book exceeds the limit, counting how many students are required.

Where people lose marks · 3
  • Failing to handle the edge case where k exceeds n; when there are more students than books, it is impossible to assign at least one book to every student.
  • Setting the search floor lower than max(pages); if a single book exceeds the ceiling, no valid student assignment can contain it.
  • Using 32-bit integers for the upper bound and running sum can overflow when array lengths reach 10^3 with elements up to 10^6.

The theory behind it

Binary Search — the ground this problem stands on. All Binary Search problems

What Binary Search is

Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.

When to reach for it

Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.

How the pattern works

Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.

What each operation costs

OperationTime
find target in sorted arrayO(log n)
find boundary in monotonic rangeO(log n)
What usually goes wrong with Binary Search
  • Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
  • Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
  • Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.

Which roles need this problem

Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

AmazonCodenationGoogleInfosysMicrosoftNPCIUber

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Search problems

Problem set and role mapping as of .