Ride-Sharing Service: Efficient Batching
Updated
Problem statement
You are building a feature for a ride-sharing service like Uber Pool. It groups ride requests into batches so that each vehicle serves several compatible riders. The goal is to use as few vehicles as possible.
Each ride is a list of three strings: [ride_id, pickup_location, pickup_time].
ride_id: a unique id. It is a string that holds a positive integer, such as"123".pickup_location: where the rider is picked up, such as"Downtown Station". It is any string.pickup_time: the pickup time in 24-hour"HH:MM"format, such as"09:30"or"14:15".
Rides can share a batch only when all of these hold:
- They have exactly the same
pickup_location. - The latest pickup time minus the earliest pickup time in the batch is at most 10 minutes. A gap of exactly 10 minutes is fine. Rides at 09:00, 09:05 and 09:10 can share a batch. A ride at 09:11 cannot join a batch that starts at 09:00.
- A batch has at most 3 rides. A batch can have 1, 2 or 3 rides.
- Each ride is in exactly one batch.
Several splits can use the same number of batches. To get one answer, build the batches like this. Handle each pickup location on its own. Sort its rides by pickup time, and put rides with the same time in input order. Start a batch with the earliest ride that is not yet in a batch. Then add the next rides in that order, as long as the batch has fewer than 3 rides and the next ride is at most 10 minutes after the first ride of the batch. Repeat until every ride is in a batch. This always uses the fewest batches.
Each batch is written as a list of its ride ids, as integers, in ascending order. List the batches in order of the smallest input index of any ride in the batch. For example, a batch holding the rides at input positions 0 and 5 comes before a batch holding the rides at positions 1 and 2.
Complete the function batchRides in the editor.
batchRides has the following parameter:
String[][] rides: the ride requests, each as[ride_id, pickup_location, pickup_time]
The function returns an int[][]: the batches, each a sorted list of ride ids, in the order described above.
Function
batchRides(rides: String[][]) → int[][]Examples
Example 1
rides = [["1", "Downtown Station", "09:00"], ["2", "Downtown Station", "09:05"], ["3", "Uptown Plaza", "10:00"], ["4", "Downtown Station", "09:12"], ["5", "Uptown Plaza", "10:07"], ["6", "Airport Terminal", "11:00"]]return = [[1, 2], [3, 5], [4], [6]]Downtown Station has rides 1 (09:00), 2 (09:05) and 4 (09:12). The batch starts at ride 1. Ride 2 is 5 minutes later, so it joins. Ride 4 is 12 minutes after ride 1, which is more than 10, so it starts its own batch. That gives [1, 2] and [4].
Uptown Plaza has rides 3 (10:00) and 5 (10:07). Ride 5 is 7 minutes after ride 3, so they share the batch [3, 5].
Airport Terminal has only ride 6, so it is alone in [6].
The batches are ordered by the smallest input index they hold: [1, 2] has index 0, [3, 5] has index 2, [4] has index 3 and [6] has index 5. The answer is [[1, 2], [3, 5], [4], [6]].
Constraints
1 ≤ N ≤ 104, whereNisrides.length- Each ride has exactly 3 strings.
- Each
ride_idis a positive integer without leading zeros, at most109. No two rides have the sameride_id. - Each
pickup_locationhas 1 to 30 characters. - Each
pickup_timeis a valid time from00:00to23:59inHH:MMformat.
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
- Hiking Club Fastest PaceInfosys · OAInfosys interview questions
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions