#2238
Medium Database Number of times a driver was a passenger
Database
71.7% acceptance
Mar 31, 2026
73
5
No description available.
Solution
Pandas
Time O(1)
Space O(1)
# Table: Rides
#
# +--------------+------+
# | Column Name | Type |
# +--------------+------+
# | ride_id | int |
# | driver_id | int |
# | passenger_id | int |
# +--------------+------+
# ride_id is the column with unique values for this table.
# Each row of this table contains the ID of the driver and the ID of the passenger that rode in ride_id.
# Note that driver_id != passenger_id.
#
#
#
# Write a solution to report the ID of each driver and the number of times they were a passenger.
#
# Return the result table in any order.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Rides table:
# +---------+-----------+--------------+
# | ride_id | driver_id | passenger_id |
# +---------+-----------+--------------+
# | 1 | 7 | 1 |
# | 2 | 7 | 2 |
# | 3 | 11 | 1 |
# | 4 | 11 | 7 |
# | 5 | 11 | 7 |
# | 6 | 11 | 3 |
# +---------+-----------+--------------+
# Output:
# +-----------+-----+
# | driver_id | cnt |
# +-----------+-----+
# | 7 | 2 |
# | 11 | 0 |
# +-----------+-----+
# Explanation:
# There are two drivers in all the given rides: 7 and 11.
# The driver with ID = 7 was a passenger two times.
# The driver with ID = 11 was never a passenger.
import pandas as pd
def driver_passenger(rides: pd.DataFrame) -> pd.DataFrame:
drivers = rides[['driver_id']].drop_duplicates()
passenger_cnt = rides.groupby('passenger_id').size().reset_index(name='cnt')
result = drivers.merge(passenger_cnt, left_on='driver_id', right_on='passenger_id', how='left')
result['cnt'] = result['cnt'].fillna(0).astype(int)
return result[['driver_id', 'cnt']]