cswork / libraryOpen practice workspace

Coding questions · Capital One

Minutes Until the Next Bus From a Daily Departure Schedule

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.

Function Signature

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

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

Input:  schedule = ["09:15", "06:40", "06:40"], current_time = "06:40"
Output: 0

A bus leaves at exactly the current time.

Example 3

Input:  schedule = ["07:00", "21:30"], current_time = "22:00"
Output: -1

Every bus has already left for the day.

Minutes Until the Next Bus Departure

Hints

  1. Converting each HH:MM string into a single number of minutes since midnight makes times easy to compare and subtract.
  2. Only departures at or after the current time count: one exactly at the current time is catchable, and the day does not wrap around.
  3. 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.

Approach

Convert every HH:MM string to minutes since midnight as hour * 60 + minute, reading each two-digit field in base 10 so leading zeros such as 08 and 09 parse correctly. Let now be current_time in minutes. Scan the schedule once: a departure d counts only when d >= now, and its wait is d - now; keep the smallest such wait, starting from -1 to mean that no departure has counted yet. Invariant: after any prefix of the schedule, best is -1 if no departure in that prefix is at or after now, and otherwise it is the minimum wait over the departures in that prefix that are. After the full scan, best is the wait for the earliest departure at or after now, which is exactly the next bus, or -1 when every departure is earlier. Correctness does not depend on order, because the result is a minimum over all elements, and duplicates cannot change a minimum. Edge cases: a departure equal to current_time gives 0; departures before current_time never wrap to the next day, so a late current_time with only early departures gives -1; the largest possible wait is 1439, from 00:00 to 23:59.

  • Time complexity: O(n)
  • Space complexity: O(1)