Elapsed Time Since the Most Recent Bus Departure

Quick Overview

Given a list of bus departure times in minutes and the current time, return how many minutes have passed since the latest bus that already left, or -1 if none has left. It tests a single linear scan with a filtered maximum and careful handling of the boundary where a bus departs exactly now.

Elapsed Time Since the Most Recent Bus Departure

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list `schedule` of bus departure times, each an integer number of minutes, and an integer `current_time`. Determine the elapsed time since the most recent bus departed. The most recent bus that has already departed is the one with the largest departure time that is less than or equal to `current_time`. Return the number of minutes that have passed since that departure. If no bus has departed yet, return `-1`. ### Function Signature ```python def elapsed_time(schedule: list[int], current_time: int) -> int: ``` ### Rules - `schedule` is not guaranteed to be sorted and may contain repeated times. - A bus whose departure time equals `current_time` has already departed, so the answer in that case is `0`. - If every departure time is greater than `current_time`, return `-1`. ### Constraints - `1 <= len(schedule) <= 10^4` - `0 <= schedule[i] <= 10^6` - `0 <= current_time <= 10^6` ### Examples ```text Input: schedule = [10, 20, 30, 40, 50], current_time = 35 Output: 5 ``` The buses at 10, 20 and 30 left at or before minute 35. The most recent of them is 30, so the elapsed time is 35 - 30 = 5. ```text Input: schedule = [100, 200, 300], current_time = 50 Output: -1 ``` Every departure (100, 200, 300) is after minute 50, so no bus has departed yet. ```text Input: schedule = [45, 5, 30, 30], current_time = 30 Output: 0 ``` The departures at or before minute 30 are 5, 30 and 30. The latest is 30, which is exactly the current time, so 0 minutes have passed.

Overview: Given a list of bus departure times in minutes and the current time, return how many minutes have passed since the latest bus that already left, or -1 if none has left. It tests a single linear scan with a filtered maximum and careful handling of the boundary where a bus departs exactly now.

Community answers

Answer by fizik

Easy problem it is: single pass. def elapsed_time(schedule: list[int], current_time: int) -> int: min_et = float("inf") for t in schedule: if current_time >= t: min_et = min(min_et, current_time - t) return -1 if min_et == float("inf") else min_et
Uber logo
Uber
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list schedule of bus departure times, each an integer number of minutes, and an integer current_time. Determine the elapsed time since the most recent bus departed.

The most recent bus that has already departed is the one with the largest departure time that is less than or equal to current_time. Return the number of minutes that have passed since that departure. If no bus has departed yet, return -1.

Function Signature

def elapsed_time(schedule: list[int], current_time: int) -> int:

Rules

  • schedule is not guaranteed to be sorted and may contain repeated times.
  • A bus whose departure time equals current_time has already departed, so the answer in that case is 0 .
  • If every departure time is greater than current_time , return -1 .

Constraints

  • 1 <= len(schedule) <= 10^4
  • 0 <= schedule[i] <= 10^6
  • 0 <= current_time <= 10^6

Examples

Input: schedule = [10, 20, 30, 40, 50], current_time = 35
Output: 5

The buses at 10, 20 and 30 left at or before minute 35. The most recent of them is 30, so the elapsed time is 35 - 30 = 5.

Input: schedule = [100, 200, 300], current_time = 50
Output: -1

Every departure (100, 200, 300) is after minute 50, so no bus has departed yet.

Input: schedule = [45, 5, 30, 30], current_time = 30
Output: 0

The departures at or before minute 30 are 5, 30 and 30. The latest is 30, which is exactly the current time, so 0 minutes have passed.