AnalystPath

Compatible Carpool Pairs

PandasHardSenior level~10 min

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.

trips
rider_idroute_idtrip_date
1102021-03-15
1112021-03-15
1122021-03-15
2102021-03-15
2112021-03-15
carpool
rider1_idrider2_id
12
27

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 trips

Solve 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

Related Pandas questions