Compatible Carpool Pairs
Problem
A commuter app records the routes each rider takes each day, and which riders are already grouped into the same carpool.
DataFrame `trips` (from `trips.csv`):
```text
+-----------+------+
| Column | Type |
+-----------+------+
| rider_id | int |
| route_id | int |
| trip_date | date |
+-----------+------+
Each row means the rider travelled that route on that date.
```
DataFrame `carpool` (from `carpool.csv`):
```text
+-----------+------+
| Column | Type |
+-----------+------+
| rider1_id | int |
| rider2_id | int |
+-----------+------+
(rider1_id, rider2_id) is the primary key. Each row means the two riders share a carpool; rider1_id < rider2_id.
```
A carpool pair `(rider1_id, rider2_id)` is **highly compatible** if:
- They already share a carpool.
- They travelled the same **three or more distinct routes on the same day**.
Return a DataFrame with columns `rider1_id` and `rider2_id` (with rider1_id < rider2_id, no duplicate rows), in any order.
Input data
Example rows — the live problem includes the full dataset.
| rider_id | route_id | trip_date |
|---|---|---|
| 1 | 10 | 2021-03-15 |
| 1 | 11 | 2021-03-15 |
| 1 | 12 | 2021-03-15 |
| 2 | 10 | 2021-03-15 |
| 2 | 11 | 2021-03-15 |
| rider1_id | rider2_id |
|---|---|
| 1 | 2 |
| 2 | 7 |
Expected output
Your answer should return 2 rows with the columns rider1_id, rider2_id.
Starter code (Pandas (Python))
import pandas as pd
def compatible_carpool_pairs(trips, carpool) -> pd.DataFrame:
# Your code here
return tripsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock