Riders per Ferry II
Problem
A harbour logs ferry departures in a DataFrame `ferries` with columns `(ferry_id, depart_time, capacity)` and rider arrivals in a DataFrame `riders` with columns `(rider_id, arrive_time)` (both loaded from CSVs). Times are integers; `ferry_id` and `rider_id` are unique.
Ferries leave in order of `depart_time`. When a ferry departs, riders who have already arrived and are still waiting board it in arrival order until either everyone waiting is aboard or the ferry hits its `capacity`; the rest keep waiting. Return `ferry_id` and `rider_count` (how many riders actually board) for every ferry, ordered by `ferry_id` ascending.
Input data
Example rows — the live problem includes the full dataset.
| ferry_id | depart_time | capacity |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 4 | 10 |
| 3 | 7 | 2 |
| rider_id | arrive_time |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 5 |
| 4 | 6 |
| 5 | 7 |
Expected output
Your answer should return 3 rows with the columns ferry_id, rider_count.
Starter code (Pandas (Python))
import pandas as pd
def riders_per_ferry_ii(ferries, riders) -> pd.DataFrame:
# Your code here
return ferriesSolve 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