KatInterview

Elapsed Minutes Since the Latest Bus Pull-Out

StringsAuto-graded · 23 test cases
EasyAuto-gradedStringstime-parsingmodular-arithmeticwrap-around

Last updated

Convert clock strings to minutes and find the shortest wrap-around gap back to a past departure, covering unsorted input, duplicates and an empty schedule.

Elapsed Minutes Since the Latest Bus Pull-Out

A depot keeps a daily timetable of pull-out times for one route. The timetable repeats every day. Each time is a 24-hour clock string "HH:MM". You are also given the current clock time as a string in the same format.

Write a function that returns how many minutes have passed since the most recent pull-out.

Rules

  • If at least one pull-out time is at or before the current time on the clock, the most recent pull-out is the latest such time today. The answer is the current time minus that time, in minutes.
  • A pull-out whose time is exactly equal to the current time counts as having just happened, so the answer is 0.
  • If every pull-out time is later in the day than the current time, nothing has left yet today. The most recent pull-out is then the latest pull-out time of yesterday, and the answer is the number of minutes from that moment, across midnight, to the current time.
  • The list may be unsorted and may contain repeated times. Repeats do not change the answer.
  • If the list of pull-out times is empty, return -1.

Input

  • schedule: a list of strings, each in "HH:MM" format.
  • now: a string in "HH:MM" format.

Output

An integer: the number of minutes since the most recent pull-out, or -1 if schedule is empty.

Constraints

  • schedule normally contains between 1 and 1000 strings; an empty list must still return -1.
  • Every time is a valid "HH:MM" between 00:00 and 23:59.
  • schedule is not guaranteed to be sorted.
  • The answer is always between 0 and 1439 when the list is non-empty.

Examples

Example 1

Input: minutes_since_last_departure(['08:30', '14:00', '18:15'], '15:00')

Output: 60

Example 2

Input: minutes_since_last_departure(['09:00', '22:00'], '06:00')

Output: 480

Example 3

Input: minutes_since_last_departure(['08:00', '08:00', '12:00'], '08:00')

Output: 0

Hints

Hint 1

Convert every time to minutes since midnight so that comparisons and subtraction become plain integer operations.

Hint 2

Split into two cases: some pull-out is at or before the current minute, or none is. In the second case the latest pull-out of the whole schedule happened yesterday.

Hint 3

A day has 1440 minutes. The gap from a pull-out at minute d to the current minute t is (t - d) modulo 1440, and you want the smallest such gap.