FastPrepAssign Campers to the First Arriving Car

Assign Campers to the First Arriving Car

Atlassian logoAtlassian● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Two cars leave their starting locations at time zero and drive to Campground. Each row of roads is [origin, destination, duration], where duration is a decimal string of travel minutes. Each location has at most one outgoing road, routes have no cycles or detours, and both cars follow their routes without waiting.

starts gives the two starting locations. Each row of people is [name, location]. The first car to reach a person's location picks them up. Return two rows of passenger names, one for car 0 and one for car 1, in the same order as starts.

For this exercise, assume a simultaneous arrival assigns the person to car 0. Within each car, order passengers by pickup time, breaking equal pickup times by their original row order in people. Cars have unlimited capacity and pickups take no time. Several people may share a location. Empty passenger lists produce empty result rows.

For this exercise, assume every road belongs to an acyclic route ending at Campground, every passenger location lies on at least one car route, and all names and locations are valid case-sensitive strings.

Function

assignCarpool(roads: String[][], starts: String[], people: String[][]) → String[][]

Examples

Example 1

roads = [["Bridgewater","Caledonia","30"],["Caledonia","New Grafton","15"],["New Grafton","Campground","5"],["Milton","New Grafton","30"],["Liverpool","Milton","10"]]starts = ["Bridgewater","Liverpool"]people = [["Jessie","Bridgewater"],["Travis","Caledonia"],["Jeremy","New Grafton"],["Katie","Liverpool"]]return = [["Jessie","Travis"],["Katie","Jeremy"]]

The literal roads and passengers put Jessie/Travis in car 0 and Katie/Jeremy in car 1.

Example 2

roads = [["A","M","5"],["B","M","5"],["M","Campground","2"]]starts = ["A","B"]people = [["Mia","M"],["Bob","B"],["Amy","A"]]return = [["Amy","Mia"],["Bob"]]

The cars tie at M, so car 0 gets Mia; pickup-time order places Amy before Mia.

Constraints

  • For this exercise, assume 0 <= roads.length <= 100, exactly two starts, and 0 <= people.length <= 100.
  • Each origin has one outgoing road; Campground has none. Starts are road endpoints or Campground.
  • Durations are decimal strings representing integers from 1 through 100000.
  • Person names are unique. Names and locations are nonempty printable ASCII strings of at most 30 characters.

More Atlassian problems

See Atlassian hiring insights
public String[][] assignCarpool(String[][] roads, String[] starts, String[][] people) {
    // Write your code here.
    return new String[2][0];
}
roads[["Bridgewater","Caledonia","30"],["Caledonia","New Grafton","15"],["New Grafton","Campground","5"],["Milton","New Grafton","30"],["Liverpool","Milton","10"]]
starts["Bridgewater","Liverpool"]
people[["Jessie","Bridgewater"],["Travis","Caledonia"],["Jeremy","New Grafton"],["Katie","Liverpool"]]
expected[["Jessie", "Travis"], ["Katie", "Jeremy"]]
Checking account…