FastPrepLatest Arrival Time for a Shuttle

Latest Arrival Time for a Shuttle

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given integer shuttle departure times shuttles, integer arrival times passengers for the other passengers, and the capacity of every shuttle.

A passenger may board a shuttle when the passenger arrives no later than that shuttle departs. Process the other passengers from earliest arrival to latest arrival. Each passenger boards the earliest shuttle that still has an available seat.

Choose an integer arrival time for yourself. Your arrival time must be different from every value in passengers. Return the latest arrival time that still lets you board some shuttle.

The input arrays may be unsorted. If the final shuttle has an empty seat after the other passengers board, its departure time is the latest initial candidate. If it is full, the latest initial candidate is one unit before the last passenger who boarded it. In either case, move the candidate earlier while another passenger already uses that time.

Function

latestShuttleArrival(shuttles: int[], passengers: int[], capacity: int) → int

Examples

Example 1

shuttles = [10,20]passengers = [2,17,18,19]capacity = 2return = 16

The shuttle at 20 fills with passengers arriving at 17 and 18. You must arrive before 18, and 17 is already occupied, so the latest available time is 16.

Example 2

shuttles = [20,30]passengers = [19,20,21,29]capacity = 2return = 28

The shuttle at 30 boards the passengers at 21 and 29. It is full, so you must arrive before 29; 28 is unused.

Example 3

shuttles = [10]passengers = [2,3]capacity = 3return = 10

One seat remains on the shuttle, and no passenger arrives at its departure time, so you may arrive at 10.

Constraints

  • 1 <= shuttles.length, passengers.length <= 10^5.
  • 1 <= capacity <= 10^5.
  • 1 <= shuttles[i], passengers[i] <= 10^9.
  • Departure times are unique within shuttles, and arrival times are unique within passengers.
  • At least one valid arrival time exists.

More Google problems

See Google hiring insights
public int latestShuttleArrival(int[] shuttles, int[] passengers, int capacity) {
  // Write your code here.
}
shuttles[10,20]
passengers[2,17,18,19]
capacity2
expected16
Checking account…