Leetcode #262: Trips and Users
In this guide, we solve Leetcode #262 Trips and Users in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
Table: Trips +-------------+----------+ | Column Name | Type | +-------------+----------+ | id | int | | client_id | int | | driver_id | int | | city_id | int | | status | enum | | request_at | varchar | +-------------+----------+ id is the primary key (column with unique values) for this table. The table holds all taxi trips.
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: Database
Intuition
The task is relational in nature, which maps cleanly to DataFrame operations in Python.
By treating tables as DataFrames, joins and group-bys become concise and readable.
Approach
Load the inputs as DataFrames and apply the appropriate merge, filter, or group-by.
Select or rename the columns to match the required output.
Steps:
- Load inputs as DataFrames.
- Apply merge/groupby/filter operations.
- Select the output columns.
Example
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| client_id | int |
| driver_id | int |
| city_id | int |
| status | enum |
| request_at | varchar |
+-------------+----------+
id is the primary key (column with unique values) for this table.
The table holds all taxi trips. Each trip has a unique id, while client_id and driver_id are foreign keys to the users_id at the Users table.
Status is an ENUM (category) type of ('completed', 'cancelled_by_driver', 'cancelled_by_client').
Python Solution
import pandas as pd
def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
# 1) temporal filtering
trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename(
columns={"request_at": "Day"}
)
# 2) filtering based not banned
# 2.1) mappning the column 'banned' to `client_id` and `driver_id`
df_client = (
pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left")
.drop(["users_id", "role"], axis=1)
.rename(columns={"banned": "banned_client"})
)
df_driver = (
pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left")
.drop(["users_id", "role"], axis=1)
.rename(columns={"banned": "banned_driver"})
)
df = pd.merge(
df_client,
df_driver,
left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"],
how="left",
)
# 2.2) filtering based on not banned
df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")]
# 3) counting the cancelled and total trips per day
df["status_cancelled"] = df["status"].str.contains("cancelled")
df = df[["Day", "status_cancelled"]]
df = df.groupby("Day").agg(
{"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]}
)
df.columns = df.columns.droplevel()
df = df.reset_index()
# 4) calculating the ratio
df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2)
return df[["Day", "Cancellation Rate"]]
Complexity
The time complexity is O(n log n) (typical). The space complexity is O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.