Quick Overview

A string-processing problem: given one day's shuttle departure times in HH:MM format, sorted chronologically, and the current time, return the minutes until the next departure as a string, or -1 when no shuttle remains that day. It tests time parsing, conversion to minutes and boundary handling.

Minutes Until the Next Shuttle From an HH:MM Departure Schedule

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

A shuttle runs on a fixed daily timetable. You are given its departure times for one day, sorted in chronological order, and the current time. Return how many minutes remain until the next shuttle departs. If no shuttle departs for the rest of the day, return `"-1"`. ### Function Signature ```python def minutes_until_next_shuttle(schedule: list[str], current_time: str) -> str: ``` ### Rules - All times use the 24-hour `"HH:MM"` format with two-digit hours and minutes. - A shuttle departing exactly at `current_time` counts as the next shuttle, with 0 minutes to wait. - Only the same day counts: the schedule does not wrap past midnight into the next day. - Return the number of minutes as a decimal string with no leading zeros, such as `"45"` or `"0"`, or `"-1"` when no shuttle remains. ### Constraints - `1 <= len(schedule) <= 10^4` - `schedule` is sorted in non-decreasing order and may contain repeated times. - Every time, including `current_time`, is a valid `"HH:MM"` string from `"00:00"` to `"23:59"`. ### Examples **Example 1** ```text Input: schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "08:10" Output: "260" ``` The next departure is at 12:30, which is 4 hours and 20 minutes, or 260 minutes, after 08:10. **Example 2** ```text Input: schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "19:00" Output: "-1" ``` The last shuttle of the day left at 18:45. **Example 3** ```text Input: schedule = ["06:00", "06:30", "06:30"], current_time = "06:30" Output: "0" ``` A shuttle departs exactly at the current time.

Overview: A string-processing problem: given one day's shuttle departure times in HH:MM format, sorted chronologically, and the current time, return the minutes until the next departure as a string, or -1 when no shuttle remains that day. It tests time parsing, conversion to minutes and boundary handling.

Read the full Capital One Machine Learning Engineer interview experience this question came from

A shuttle runs on a fixed daily timetable. You are given `schedule`, the shuttle's departure times for one day sorted in chronological order, and `current_time`, the current time. Return how many minutes remain until the next shuttle departs. If no shuttle departs for the rest of the day, return `"-1"`. **Rules** - All times use the 24-hour `"HH:MM"` format with two-digit hours and minutes. - A shuttle departing exactly at `current_time` counts as the next shuttle, with 0 minutes to wait. - Only the same day counts: the schedule does not wrap past midnight into the next day. - Return the number of minutes as a decimal string with no leading zeros, such as `"45"` or `"0"`, or `"-1"` when no shuttle remains. **Example 1** ``` Input: schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "08:10" Output: "260" ``` The next departure is at 12:30, which is 4 hours and 20 minutes, or 260 minutes, after 08:10. **Example 2** ``` Input: schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "19:00" Output: "-1" ``` The last shuttle of the day left at 18:45. **Constraints** - `1 <= len(schedule) <= 10^4` - `schedule` is sorted in non-decreasing order and may contain repeated times. - Every time, including `current_time`, is a valid `"HH:MM"` string from `"00:00"` to `"23:59"`. No value exceeds 2^31 - 1: a time is at most 1439 minutes after midnight, so 32-bit integers suffice in Java and C++.

Constraints

  • 1 <= len(schedule) <= 10^4
  • schedule is sorted in non-decreasing order and may contain repeated times.
  • Every time, including current_time, is a valid "HH:MM" string from "00:00" to "23:59".

Examples

Input: (['07:15', '08:00', '12:30', '18:45'], '08:10')

Expected Output: '260'

Explanation: Source example 1: 08:10 is 490 minutes and the next departure 12:30 is 750, so 750 - 490 = 260 (hour borrow from 08:10 to 12:30).

Input: (['07:15', '08:00', '12:30', '18:45'], '19:00')

Expected Output: '-1'

Explanation: Source example 2: the last departure 18:45 is before 19:00 and nothing wraps to the next day.

Hints

  1. Turn each "HH:MM" time into a single number, such as the minutes elapsed since midnight, so two times can be compared and subtracted directly.
  2. Only the earliest departure that is not earlier than current_time matters, and a departure exactly at current_time counts with a wait of 0.
  3. Nothing carries over past midnight: if every departure is earlier than current_time, the answer is "-1".

Minutes Until the Next Shuttle Departure

hardCapital OneMachine Learning Engineer

A shuttle runs on a fixed daily timetable. You are given schedule, the shuttle's departure times for one day sorted in chronological order, and current_time, the current time. Return how many minutes remain until the next shuttle departs. If no shuttle departs for the rest of the day, return "-1".

Rules

  • All times use the 24-hour "HH:MM" format with two-digit hours and minutes.
  • A shuttle departing exactly at current_time counts as the next shuttle, with 0 minutes to wait.
  • Only the same day counts: the schedule does not wrap past midnight into the next day.
  • Return the number of minutes as a decimal string with no leading zeros, such as "45" or "0", or "-1" when no shuttle remains.

Example 1

Input:  schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "08:10"
Output: "260"

The next departure is at 12:30, which is 4 hours and 20 minutes, or 260 minutes, after 08:10.

Example 2

Input:  schedule = ["07:15", "08:00", "12:30", "18:45"], current_time = "19:00"
Output: "-1"

The last shuttle of the day left at 18:45.

Constraints

  • 1 <= len(schedule) <= 10^4
  • schedule is sorted in non-decreasing order and may contain repeated times.
  • Every time, including current_time, is a valid "HH:MM" string from "00:00" to "23:59".

No value exceeds 2^31 - 1: a time is at most 1439 minutes after midnight, so 32-bit integers suffice in Java and C++.

Examples

Example 1
Input
(['07:15', '08:00', '12:30', '18:45'], '08:10')
Output
'260'
Notes
Source example 1: 08:10 is 490 minutes and the next departure 12:30 is 750, so 750 - 490 = 260 (hour borrow from 08:10 to 12:30).
Example 2
Input
(['07:15', '08:00', '12:30', '18:45'], '19:00')
Output
'-1'
Notes
Source example 2: the last departure 18:45 is before 19:00 and nothing wraps to the next day.

Constraints

  • •1 <= len(schedule) <= 10^4
  • •schedule is sorted in non-decreasing order and may contain repeated times.
  • •Every time, including current_time, is a valid "HH:MM" string from "00:00" to "23:59".

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def minutes_until_next_shuttle(schedule: list[str], current_time: str) -> str:
Sample call
minutes_until_next_shuttle(['07:15', '08:00', '12:30', '18:45'], '08:10')
Language
Case 1
Input
(['07:15', '08:00', '12:30', '18:45'], '08:10')
Expected
'260'
Case 2
Input
(['07:15', '08:00', '12:30', '18:45'], '19:00')
Expected
'-1'

Browse More Questions

Show the approach

Approach

Convert each "HH:MM" time to minutes since midnight, hours * 60 + minutes, reading both two-digit fields as decimal (so "08" is 8). The next shuttle is the first departure whose minute value is greater than or equal to the current time's. Because schedule is sorted in non-decreasing order, a lower-bound binary search finds it. Keep a half-open range [lo, hi) with the invariant that every index before lo departs strictly before current_time and every index at or after hi departs at or after it. Compare the middle departure with current_time and discard the half that cannot hold the answer. When lo == hi, index lo is the first departure at or after current_time. If lo equals len(schedule), every departure is earlier, and because the timetable does not wrap past midnight the answer is "-1". Otherwise the answer is that departure's minutes minus the current minutes, converted with the language's integer-to-string routine, which never emits leading zeros. Edge cases: a departure equal to current_time gives "0" because the test is >=, not >. Duplicated times need no special handling because every entry in an equal run has the same value. A current_time before the first departure returns the gap to schedule[0], and the largest possible answer is "1439" (from "00:00" to "23:59"). A linear scan for the first departure at or after current_time is equally correct for n <= 10^4.

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