InternDoor

Share the books fairly

Hardbinary search · greedy · arrays

Reported in interviews at (where)

The books on a shelf stand in a fixed order, and pages[i] is the number of pages in book i. Share them among readers people so that:

each person gets a run of books standing next to each other on the shelf, every person gets at least one book, and every book goes to someone.

Make the largest number of pages any one person has to read as small as possible, and return that number.

Example 1
Input
pages = [10,20,30,40]
readers = 2
Output
60

Giving 10, 20 and 30 to one person and 40 to the other means at most 60 pages each. Splitting after 20 instead gives one person 70.

Example 2
Input
pages = [5,5,5]
readers = 3
Output
5

One book each.

Example 3
Input
pages = [3,1,4,1,5]
readers = 1
Output
14

One person reads everything.

Limits

  • 1 <= readers <= pages.length <= 20000
  • 1 <= pages[i] <= 1000000000

Hints

Hint 1

Turn the question round: for a limit L, can the books be shared so nobody reads more than L pages? Walking the shelf and starting a new person whenever the next book would pass L gives the fewest people needed.

Hint 2

If L works, every larger L works too. Binary search L between the biggest single book and the total number of pages.

Where this was reported

Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.

Run your code to check it on the examples. Submit runs every hidden test.

Ready for the real one?

Engineering internships and entry-level jobs in India, newest first.