Find the Next Bus Departure Across Multiple Sorted Schedules

Quick Overview

Given several bus lines, each with a sorted list of departure times in minutes from midnight, return the earliest departure at or after the current time across all lines, or -1 if none remains. It tests a lower-bound binary search on each sorted list and correct handling of an exact time match.

Find the Next Bus Departure Across Multiple Sorted Schedules

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given multiple bus lines. Each line has a sorted list of departure times, in minutes from midnight, ranging from `0` to `1440`. You are also given `current_time`. Return the next available departure time across all bus lines that is at or after `current_time`. If `current_time` is exactly equal to a departure time, that departure is still considered available. If no departure is available at or after `current_time` on any line, return `-1`. ### Function Signature ```python def next_departure(lines: list[list[int]], current_time: int) -> int: ``` ### Rules - `lines[i]` is the departure list of line `i`, sorted in non-decreasing order. - Return the departure time itself (a minute of the day), not the number of minutes to wait. - A departure at exactly `current_time` counts as available. - Return `-1` when every departure on every line is earlier than `current_time`. ### Constraints - `1 <= len(lines) <= 100` - `1 <= len(lines[i]) <= 10^5` - `0 <= lines[i][j] <= 1440` - Each `lines[i]` is sorted in non-decreasing order. - `0 <= current_time <= 1440` ### Examples ```text Input: lines = [[300, 420, 540, 660, 780, 900], [350, 470, 590, 710]], current_time = 500 Output: 540 ``` On the first line the next departure at or after 500 is 540. On the second line it is 590. The earliest across both lines is 540. ```text Input: lines = [[60, 120, 180], [90, 150, 210]], current_time = 150 Output: 150 ``` The first line's next departure is 180. The second line has a departure at exactly 150, which still counts, so the answer is 150. ```text Input: lines = [[100, 200], [50]], current_time = 250 Output: -1 ``` Every departure (100, 200 and 50) is before minute 250, so no departure is available on any line.

Overview: Given several bus lines, each with a sorted list of departure times in minutes from midnight, return the earliest departure at or after the current time across all lines, or -1 if none remains. It tests a lower-bound binary search on each sorted list and correct handling of an exact time match.

Instacart logo
Instacart
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given multiple bus lines. Each line has a sorted list of departure times, in minutes from midnight, ranging from 0 to 1440. You are also given current_time.

Return the next available departure time across all bus lines that is at or after current_time. If current_time is exactly equal to a departure time, that departure is still considered available. If no departure is available at or after current_time on any line, return -1.

Function Signature

def next_departure(lines: list[list[int]], current_time: int) -> int:

Rules

  • lines[i] is the departure list of line i , sorted in non-decreasing order.
  • Return the departure time itself (a minute of the day), not the number of minutes to wait.
  • A departure at exactly current_time counts as available.
  • Return -1 when every departure on every line is earlier than current_time .

Constraints

  • 1 <= len(lines) <= 100
  • 1 <= len(lines[i]) <= 10^5
  • 0 <= lines[i][j] <= 1440
  • Each lines[i] is sorted in non-decreasing order.
  • 0 <= current_time <= 1440

Examples

Input: lines = [[300, 420, 540, 660, 780, 900], [350, 470, 590, 710]], current_time = 500
Output: 540

On the first line the next departure at or after 500 is 540. On the second line it is 590. The earliest across both lines is 540.

Input: lines = [[60, 120, 180], [90, 150, 210]], current_time = 150
Output: 150

The first line's next departure is 180. The second line has a departure at exactly 150, which still counts, so the answer is 150.

Input: lines = [[100, 200], [50]], current_time = 250
Output: -1

Every departure (100, 200 and 50) is before minute 250, so no departure is available on any line.