Compute minutes since last train departure
Company: Capital One
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
You are given a daily train timetable as a list of departure times (24-hour format `HH:MM`). Given the current time (also `HH:MM`), find the **most recent departure time that is not later than the current time**, and return how many minutes have elapsed since that departure.
If there is **no departure earlier than or equal to the current time** on the same day, treat it as wrapping to the **previous day’s last departure**.
### Input
- `departures`: list of strings, each in `HH:MM` (not necessarily sorted)
- `now`: string in `HH:MM`
### Output
Return:
- `last_departure`: the departure time (as `HH:MM`) that most recently occurred relative to `now`
- `minutes_ago`: the number of minutes between `last_departure` and `now`
### Example
- `departures = ["01:10", "03:20", "05:20"]`, `now = "03:40"`
- `last_departure = "03:20"`, `minutes_ago = 20`
### Constraints (assume typical interview constraints)
- `1 <= len(departures) <= 10^5`
- Times are valid and within a 24-hour day.
Quick Answer: This question evaluates time arithmetic and handling of cyclic day-wrap boundary conditions along with parsing and comparison of timestamp lists, reflecting skills in correct edge-case reasoning and efficient data processing.
Given daily departure times and current time, return the most recent departure not later than now, wrapping to previous day when needed.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: (['01:10','03:20','05:20'], '03:40')
Expected Output: {'last_departure': '03:20', 'minutes_ago': 20}
Explanation: Prompt example.
Input: (['23:50','00:10'], '00:05')
Expected Output: {'last_departure': '23:50', 'minutes_ago': 15}
Explanation: Wrap to previous day.
Input: (['12:00'], '12:00')
Expected Output: {'last_departure': '12:00', 'minutes_ago': 0}
Explanation: Exact departure.
Hints
- Choose a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.