FastPrepEarliest Car for Each Passenger
Problems/Earliest Car for Each Passenger

Earliest Car for Each Passenger

WhatNot logoWhatNot● MediumFULLTIMENEW GRADPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report gives cars with fixed routes and passengers who must take the earliest compatible car. The ordered-subsequence rule is the smallest deterministic interpretation of a route serving all requested attractions.

Cars depart in the order given. Each car follows a fixed route represented by the attractions it visits in travel order. Each passenger supplies an ordered list of attractions they want to visit.

Assign each passenger to the earliest departing car whose route contains the passenger's entire requested list as a subsequence. Return the assigned zero-based car index for every passenger, or -1 when no route can serve that passenger.

Function

assignEarliestCars(carRoutes: int[][], passengerStops: int[][]) → int[]

Examples

Example 1

carRoutes = [[1,3,5,7],[1,2,3,4],[2,5,7]]passengerStops = [[1,5,7],[1,4],[5,2],[]]return = [0,1,-1,0]

The first two requests are served by cars 0 and 1. No route visits 5 before 2.

Example 2

carRoutes = [[4,8],[4,6,8],[8]]passengerStops = [[4,8],[8]]return = [0,0]

Car 0 is the earliest compatible route for both passengers.

Example 3

carRoutes = []passengerStops = [[],[1]]return = [-1,-1]

There is no car to assign.

Constraints

  • 0 <= carRoutes.length, passengerStops.length <= 500.
  • Each route and requested list contains at most 500 integer attraction IDs.
  • The total number of route and request entries is at most 100000.
  • A passenger with an empty request can take the first car, or receives -1 when there are no cars.

More WhatNot problems

  • User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Adjacent Pair and Run RemovalPHONE SCREEN · Seen Oct 2026
  • Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public int[] assignEarliestCars(int[][] carRoutes, int[][] passengerStops) {
        // Write your solution here.
    }
}
Checking account…