Evenings Needed to Read Chapters in Order Under Nightly Time Budgets
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Online Assessment
A book has M chapters that must be read in order, from the first chapter to the last, without skipping any. Reading chapter K takes `chapter[K]` minutes, and every chapter must be read completely within a single evening. A reader has planned N evenings; on evening J there are `evening[J]` minutes available for reading. On each evening the reader reads as many whole chapters as fit in that evening's time. Return the number of evenings needed to finish the book, or -1 if the book cannot be finished within the planned evenings.
### Function Signature
```python
def evenings_needed(chapter: list[int], evening: list[int]) -> int:
```
### Rules
- Evenings are used in order, starting with `evening[0]`, and chapters are read in order, starting with `chapter[0]`.
- At the start of an evening the reader has that evening's full minutes. While an unread chapter remains and the next chapter's length is less than or equal to the minutes left that evening, the reader reads that whole chapter and subtracts its length from the minutes left.
- As soon as the next chapter does not fit in the minutes left, the evening ends. The reader never starts a chapter that cannot be finished that evening, and leftover minutes do not carry over to later evenings.
- Return the 1-based number of the evening during which the last chapter is finished. Evenings on which no chapter fits still count.
- Return -1 if any chapter is still unread after the last planned evening.
### Constraints
The report gives no size or value bounds for this task; this version assumes:
- `1 <= M = len(chapter) <= 10^5`
- `1 <= N = len(evening) <= 10^5`
- `1 <= chapter[K] <= 10^4`
- `1 <= evening[J] <= 10^4`
### Examples
**Example 1**
```text
Input: chapter = [3, 2, 1], evening = [2, 5, 3, 3]
Output: 3
```
Evening 1 has 2 minutes, not enough for the 3-minute first chapter, so nothing is read. Evening 2 has 5 minutes: the first chapter (3) and the second (2) fit exactly, leaving 0 minutes, so the third chapter waits. Evening 3 has 3 minutes and the last chapter (1) is read, so the book is finished on evening 3.
**Example 2**
```text
Input: chapter = [5, 8, 5], evening = [9, 4, 5, 3, 8]
Output: -1
```
Evening 1 reads the first chapter (5) and has 4 minutes left, too few for the 8-minute second chapter. Evenings 2, 3 and 4 have 4, 5 and 3 minutes, so nothing is read. Evening 5 has exactly 8 minutes and reads the second chapter. The third chapter is never read, so the answer is -1.
Overview: A simulation task: read a book's chapters strictly in order, fitting only whole chapters into each evening's available minutes, and report the evening on which the book is finished, or -1 if the planned evenings run out. It tests precise rule-following, including exact fits, lost leftover minutes and evenings where nothing can be read.
Read the full Google Software Engineer interview experience this question came from