FastPrepUber Circular Shuttle Optimization
Problems/Uber Circular Shuttle Optimization

Uber Circular Shuttle Optimization

Uber logoUber● HardOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

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 most 2 roads 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] = 1 if a passenger waits at location i, else 0
  • int[][] edges: edges[i] = [ai, bi] is a road between ai and bi

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[][]) → int

Examples

Example 1

passengers = [1, 0, 0, 0, 0, 1]edges = [[0,1],[1,2],[2,3],[3,4],[4,5]]return = 2

The roads form the path 0 - 1 - 2 - 3 - 4 - 5. One way to reach cost 2:

  1. Start at location 2 and collect the passenger at location 0 (2 roads away).
  2. Move to location 3 (cost 1) and collect the passenger at location 5 (2 roads away).
  3. 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 = 2

One way to reach cost 2:

  1. Start at location 0 and collect the passengers at locations 3 and 4 (2 roads away, through location 1).
  2. Move to location 2 (cost 1) and collect the passenger at location 7 (2 roads away, through location 5).
  3. 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, where n = passengers.length
  • 0 ≤ passengers[i] ≤ 1
  • edges.length = n - 1
  • edges[i].length = 2
  • 0 ≤ ai, bi < n
  • ai ≠ 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
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public int minimumShuttleCost(int[] passengers, int[][] edges) {
      // write your code here
    }

}
Checking account…