Uber Circular Shuttle Optimization
Updated
Problem statement
Uber has launched a new circular shuttle service that starts and ends at the same location while collecting passengers from various stops.
The service area has n pickup locations, numbered from 0 to n - 1. They are connected by n - 1 roads, where edges[i] = [ai, bi] is a two-way road between locations ai and bi. The roads form a tree (connected, no cycles).
passengers[i] is 1 if a passenger is waiting at location i, and 0 otherwise.
The shuttle may start at any location. At each step it can do one of two things:
- Passenger collection (cost
0): collect every passenger whose location is at most2roads away from the current location. - Movement (cost
1): drive along one road to a neighboring location.
Every time the shuttle drives along a road, it costs 1, so driving along the same road twice costs 2. Return the minimum total cost to collect all passengers and come back to the starting location.
Complete the function minimumShuttleCost in the editor.
minimumShuttleCost has the following parameters:
int[] passengers:passengers[i] = 1if a passenger waits at locationi, else0int[][] edges:edges[i] = [ai, bi]is a road betweenaiandbi
The function returns an int: the minimum cost to start at some location, collect every passenger, and return to that same location.
Function
minimumShuttleCost(passengers: int[], edges: int[][]) → intExamples
Example 1
passengers = [1, 0, 0, 0, 0, 1]edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]return = 2The roads form the path 0 - 1 - 2 - 3 - 4 - 5. One way to reach cost 2:
- Start at location 2 and collect the passenger at location 0 (2 roads away).
- Move to location 3 (cost 1) and collect the passenger at location 5 (2 roads away).
- Move back to location 2 (cost 1).
No single location is within 2 roads of both 0 and 5, so the shuttle must move, and any round trip costs at least 2. Total cost = 2.
Example 2
passengers = [0, 0, 0, 1, 1, 0, 0, 1]edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]]return = 2One way to reach cost 2:
- Start at location 0 and collect the passengers at locations 3 and 4 (2 roads away, through location 1).
- Move to location 2 (cost 1) and collect the passenger at location 7 (2 roads away, through location 5).
- Move back to location 0 (cost 1).
No single location is within 2 roads of locations 3, 4 and 7 together, so the cost cannot be 0. Total cost = 2.
Constraints
1 ≤ n ≤ 3 * 104, wheren = passengers.length0 ≤ passengers[i] ≤ 1edges.length = n - 1edges[i].length = 20 ≤ ai, bi < nai ≠ bi- The roads form a tree: every two locations are joined by exactly one path.
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Binary Tree CamerasAmazon · OA · PHONE SCREEN
- Collect Opportunity Data in a TreeSalesforce · OASalesforce interview questions
- Collect CoinsCisco · OACisco interview questions
- Minimum Flips to Match Expected Binary Values in a TreeUKG · OAUKG interview questions
- Save the UniverseRubrik · OARubrik interview questions
- Equalize Server LatencyMicrosoft · OAMicrosoft interview questions
- Equalize Server LatencyTikTok · OATikTok interview questions
- Limit an Organization Tree's HeightRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Part 2: Batch Access ReviewScale AI · OAScale AI interview questions