Number of Times a Driver Was a Passenger - Problem

In a ride-sharing service, we have a database table Rides that tracks all completed trips. Each row represents a single ride with three key pieces of information:

  • ride_id - unique identifier for each ride
  • driver_id - ID of the person who drove
  • passenger_id - ID of the person who was the passenger

Your task: Some people in the system act as both drivers and passengers at different times. We need to find out for each person who has been a driver, how many times they have also been a passenger in other rides.

Output format: Return a result table with two columns:

  • driver_id - ID of each driver
  • cnt - number of times that driver was also a passenger

Note: If a driver has never been a passenger, they should still appear in the result with a count of 0.

Input & Output

example_1.sql โ€” Basic Example
$ Input: Rides table: +----------+----------+--------------+ | ride_id | driver_id | passenger_id | +----------+----------+--------------+ | 1 | 10 | 20 | | 2 | 20 | 10 | | 3 | 30 | 40 | +----------+----------+--------------+
โ€บ Output: +----------+-----+ | driver_id | cnt | +----------+-----+ | 10 | 1 | | 20 | 1 | | 30 | 0 | +----------+-----+
๐Ÿ’ก Note: Driver 10 was a passenger 1 time (in ride 2), driver 20 was a passenger 1 time (in ride 1), and driver 30 was never a passenger (cnt = 0).
example_2.sql โ€” Multiple Passenger Rides
$ Input: Rides table: +----------+----------+--------------+ | ride_id | driver_id | passenger_id | +----------+----------+--------------+ | 1 | 10 | 20 | | 2 | 30 | 10 | | 3 | 30 | 20 | | 4 | 40 | 10 | +----------+----------+--------------+
โ€บ Output: +----------+-----+ | driver_id | cnt | +----------+-----+ | 10 | 0 | | 30 | 0 | | 40 | 0 | +----------+-----+
๐Ÿ’ก Note: None of the drivers (10, 30, 40) appear as passengers in any ride, so all have cnt = 0.
example_3.sql โ€” Same Person Multiple Roles
$ Input: Rides table: +----------+----------+--------------+ | ride_id | driver_id | passenger_id | +----------+----------+--------------+ | 1 | 10 | 20 | | 2 | 20 | 10 | | 3 | 10 | 30 | | 4 | 40 | 10 | +----------+----------+--------------+
โ€บ Output: +----------+-----+ | driver_id | cnt | +----------+-----+ | 10 | 2 | | 20 | 1 | | 40 | 0 | +----------+-----+
๐Ÿ’ก Note: Driver 10 was a passenger 2 times (rides 2 and 4), driver 20 was a passenger 1 time (ride 1), and driver 40 was never a passenger.

Visualization

Tap to expand
๐Ÿš— Ride-Sharing Analysis Process๐Ÿ‘คDriver 10๐Ÿ‘คDriver 20๐Ÿ‘คDriver 30๐Ÿš™ Ride RecordsRide 1Driver: 10Passenger: 20Ride 2Driver: 20Passenger: 10Ride 3Driver: 30Passenger: 40Driver 10 as passenger?Driver 20 as passenger?Driver 30 as passenger?ResultDriver 10Count: 1โœ“ Found in Ride 2ResultDriver 20Count: 1โœ“ Found in Ride 1ResultDriver 30Count: 0โœ— Never a passenger๐Ÿ“Š Final Outputdriver_id | cnt 10 | 1 20 | 1 30 | 0
Understanding the Visualization
1
Identify All Drivers
Extract unique driver IDs from all rides - these are our people of interest
2
Match with Passenger Records
For each driver, find all rides where they appear as a passenger
3
Count and Preserve Zeros
Count passenger appearances, ensuring drivers with zero passenger rides still appear in results
Key Takeaway
๐ŸŽฏ Key Insight: Use LEFT JOIN to preserve all drivers in the result, ensuring those who were never passengers still appear with count = 0

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single pass through data with efficient join operations, typically O(n log n) due to internal sorting for GROUP BY

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for result set plus temporary space for join operations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค rides.length โ‰ค 104
  • 1 โ‰ค ride_id, driver_id, passenger_id โ‰ค 106
  • driver_id โ‰  passenger_id for each ride
  • ride_id values are unique
Asked in
Uber 45 Lyft 30 Amazon 25 DoorDash 20
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen