Minutes Until the Next Bus From a Daily Departure Schedule
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
A bus stop publishes the departure time of every bus for one day. Given that schedule and the current time, return how many minutes you have to wait until the next bus departs.
### Function Signature
```python
def minutes_until_next_bus(schedule: list[str], current_time: str) -> int:
```
### Rules
- Every time, in `schedule` and in `current_time`, is a 24-hour clock string `HH:MM`: a two-digit hour from `00` to `23`, a colon, and a two-digit minute from `00` to `59`.
- A bus that departs exactly at `current_time` can still be caught, so the wait is `0`.
- The schedule covers a single day and does not repeat. Only departures at or after `current_time` on the same day count; if there are none, return `-1`.
- `schedule` may be in any order and may list the same time more than once.
- Return the wait as a whole number of minutes.
### Constraints
- `1 <= len(schedule) <= 10^4`
- Every time lies between `00:00` and `23:59` inclusive.
- The answer is `-1` or an integer from `0` to `1439`.
### Examples
**Example 1**
```text
Input: schedule = ["08:00", "12:30", "17:45"], current_time = "11:50"
Output: 40
```
The next departure is `12:30`, which is 40 minutes after `11:50`.
**Example 2**
```text
Input: schedule = ["09:15", "06:40", "06:40"], current_time = "06:40"
Output: 0
```
A bus leaves at exactly the current time.
**Example 3**
```text
Input: schedule = ["07:00", "21:30"], current_time = "22:00"
Output: -1
```
Every bus has already left for the day.
Overview: Given one day's bus departure times as HH:MM strings and the current time, return how many minutes remain until the next departure, or -1 when no bus is left that day. Tests time parsing, handling of unsorted and duplicate schedule entries, and exact boundary rules such as a bus leaving right now.
A bus stop publishes the departure time of every bus for one day. Given that schedule and the current time, return how many minutes you have to wait until the next bus departs.
Implement `minutes_until_next_bus(schedule, current_time)`, where `schedule` is a list of departure times and `current_time` is a single time.
### Rules
- Every time, in `schedule` and in `current_time`, is a 24-hour clock string `HH:MM`: a two-digit hour from `00` to `23`, a colon, and a two-digit minute from `00` to `59`.
- A bus that departs exactly at `current_time` can still be caught, so the wait is `0`.
- The schedule covers a single day and does not repeat. Only departures at or after `current_time` on the same day count; if there are none, return `-1`. Departures earlier than `current_time` never wrap around to the next day.
- `schedule` may be in any order and may list the same time more than once.
- Return the wait as a whole number of minutes: the number of minutes from `current_time` to the earliest departure that counts.
### Constraints
- `1 <= len(schedule) <= 10^4`
- Every time lies between `00:00` and `23:59` inclusive.
- The answer is `-1` or an integer from `0` to `1439`, so every value fits in a signed 32-bit integer.
### Examples
**Example 1**
```text
Input: schedule = ["08:00", "12:30", "17:45"], current_time = "11:50"
Output: 40
```
The next departure is `12:30`, which is 40 minutes after `11:50`.
**Example 2**
```text
Input: schedule = ["07:00", "21:30"], current_time = "22:00"
Output: -1
```
Every bus has already left for the day.
Constraints
- 1 <= len(schedule) <= 10^4
- Every time in schedule and current_time is an HH:MM string with a two-digit hour 00-23 and a two-digit minute 00-59, so it lies between 00:00 and 23:59 inclusive.
- schedule may be in any order and may contain duplicate times.
- The answer is -1 or an integer from 0 to 1439, so every value fits in a signed 32-bit integer.
Examples
Input: (['08:00', '12:30', '17:45'], '11:50')
Expected Output: 40
Explanation: Source example 1: 12:30 is the next departure, 40 minutes after 11:50.
Input: (['09:15', '06:40', '06:40'], '06:40')
Expected Output: 0
Explanation: Source example 2: a duplicated departure exactly at the current time is catchable, so the wait is 0.
Hints
- Converting each HH:MM string into a single number of minutes since midnight makes times easy to compare and subtract.
- Only departures at or after the current time count: one exactly at the current time is catchable, and the day does not wrap around.
- The schedule is not guaranteed to be sorted and may repeat times, so the first qualifying entry in the list is not necessarily the next bus.