AnalystPath

Riders per Ferry II

PandasHardSenior level~10 min

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.

ferries
ferry_iddepart_timecapacity
121
2410
372
riders
rider_idarrive_time
11
21
35
46
57

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 ferries

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