Problem

Previous Bus Departure

Learn this problem
Visa logoVisaFULLTIMEOA

Problem statement

You are given an array departures of bus departure times in chronological order. Each time is in HH:MM 24-hour format.

You are also given the current time currentTime in the same format. Return the departure time of the latest bus that departed strictly before currentTime. If no bus departed before currentTime, return "-1".

Function

previousBusDeparture(departures: String[], currentTime: String) → String

Examples

Example 1

departures = ["08:15", "09:45", "11:30", "14:00"]currentTime = "12:00"return = "11:30"

The latest departure strictly before 12:00 is 11:30.

Example 2

departures = ["08:15", "09:45"]currentTime = "08:15"return = "-1"

A bus at exactly currentTime is not before the current time, and there is no earlier departure.

Constraints

  • Each time string is in HH:MM 24-hour format.
  • departures is sorted in chronological order.

More Visa problems

drafts saved locally
public String previousBusDeparture(String[] departures, String currentTime) {
  // write your code here
}
departures["08:15", "09:45", "11:30", "14:00"]
currentTime"12:00"
expected"11:30"
checking account