Travelers per Consecutive City Triplet
Updated
Problem statement
A trip log has n rows. Row i says that the traveler travelers[i] traveled to the city destinations[i] at time timestamps[i]. The rows are not in any particular order.
Put the trips of one traveler in time order. Every three cities that this traveler visited one right after another form a triplet (cityA, cityB, cityC).
For every triplet that appears in the log, count the number of different travelers who have it. A traveler who has the same triplet more than once still counts once.
Return one row [cityA, cityB, cityC, count] for each triplet, with the count written as a decimal string. Sort the rows by cityA, then by cityB, then by cityC. If there is no triplet, return an empty array.
Function
countTripletTravelers(travelers: String[], timestamps: int[], destinations: String[]) → String[][]Examples
Example 1
travelers = ["ann","bob","ann","bob","ann","bob","ann"]timestamps = [3,7,1,2,4,5,2]destinations = ["oslo","oslo","paris","paris","paris","rome","rome"]return = [["paris","rome","oslo","2"],["rome","oslo","paris","1"]]Sorted by time, ann goes to paris, rome, oslo, paris, so her triplets are (paris, rome, oslo) and (rome, oslo, paris). Bob goes to paris, rome, oslo, so his only triplet is (paris, rome, oslo). Two travelers have (paris, rome, oslo) and one has (rome, oslo, paris).
Example 2
travelers = ["dan","eve","dan","dan","eve","dan","dan"]timestamps = [10,4,30,20,9,40,50]destinations = ["tokyo","lima","tokyo","seoul","quito","seoul","tokyo"]return = [["seoul","tokyo","seoul","1"],["tokyo","seoul","tokyo","1"]]Dan goes to tokyo, seoul, tokyo, seoul, tokyo. His triplets are (tokyo, seoul, tokyo), (seoul, tokyo, seoul) and (tokyo, seoul, tokyo) again. Dan is one traveler, so (tokyo, seoul, tokyo) has a count of 1, not 2. Eve has only two trips, so she has no triplet.
Example 3
travelers = ["kim","lee","kim"]timestamps = [5,5,8]destinations = ["cairo","dubai","delhi"]return = []No traveler has three trips, so there is no triplet and the answer is empty.
Constraints
1 <= n <= 2000, wherenis the length oftravelers,timestampsanddestinations1 <= travelers[i].length, destinations[i].length <= 10travelers[i]anddestinations[i]consist of lowercase English letters.0 <= timestamps[i] <= 109- The timestamps of any one traveler are all different.
More Uber Onsite Interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Serialize and Deserialize an N-Ary TreeONSITE INTERVIEW · Seen Oct 2026
- Longest Continuous Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Oct 2026
- K Smallest Squares from a Sorted ArrayONSITE INTERVIEW · Seen Sep 2026
- Grid Robot Route with Charging PrioritiesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Increasing Path to a TargetONSITE INTERVIEW · Seen Sep 2026
- My Calendar IONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEWPostman interview questions
- Group AnagramsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Group AnagramsMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions
- Group AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Group AnagramsExpedia · ONSITE INTERVIEWExpedia interview questions