Count Evenings Needed to Finish Whole Chapters in Order

Quick Overview

Simulate ordered whole-chapter reading across evening time budgets. Count idle evenings, preserve exact-fit behavior, and derive linear total work from a single advancing chapter pointer.

Count Evenings Needed to Finish Whole Chapters in Order

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

A book has chapters with given reading durations in order. Each evening provides a time budget. You may begin a chapter only if you can finish it that evening; unused time does not carry forward. Read chapters in order and read as many whole chapters as fit each evening. Return the number of evenings elapsed when the book is finished. ### Constraints and Clarifications Array values, not array indices, represent durations and time budgets. Use nonnegative integer durations and budgets as a practice domain; no size bounds are reported. An empty book takes zero evenings. If the supplied evening budgets end before the book is complete, return-1 as an explicit interface convention because the report does not specify that case. Do not skip an oversized next chapter to read later ones. ### Example For chapters=[3,2,1] and evenings=[2,5,3,3], return3. The first evening cannot fit chapter1. The second fits the first two chapters, and the third finishes the last chapter. ### What a Strong Answer Covers Maintain the next unread chapter and reset the available time each evening. Count evenings with no progress, handle exact fits, and explain why each chapter is processed at most once. ### Follow-up Questions - Why is it incorrect to subtract an evening's budget from a partially read chapter? - How would the task change if chapters could be split across evenings?

Overview: Simulate ordered whole-chapter reading across evening time budgets. Count idle evenings, preserve exact-fit behavior, and derive linear total work from a single advancing chapter pointer.

Google logo
Google
Oct 22, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
1
0

A book has chapters with given reading durations in order. Each evening provides a time budget. You may begin a chapter only if you can finish it that evening; unused time does not carry forward. Read chapters in order and read as many whole chapters as fit each evening. Return the number of evenings elapsed when the book is finished.

Constraints and Clarifications

Array values, not array indices, represent durations and time budgets. Use nonnegative integer durations and budgets as a practice domain; no size bounds are reported. An empty book takes zero evenings. If the supplied evening budgets end before the book is complete, return-1 as an explicit interface convention because the report does not specify that case. Do not skip an oversized next chapter to read later ones.

Example

For chapters=[3,2,1] and evenings=[2,5,3,3], return3. The first evening cannot fit chapter1. The second fits the first two chapters, and the third finishes the last chapter.

What a Strong Answer Covers Guidance

Maintain the next unread chapter and reset the available time each evening. Count evenings with no progress, handle exact fits, and explain why each chapter is processed at most once.

Follow-up Questions Guidance

  • Why is it incorrect to subtract an evening's budget from a partially read chapter?
  • How would the task change if chapters could be split across evenings?