Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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. 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').
Table: Users
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| users_id | int |
| banned | enum |
| role | enum |
+-------------+----------+
users_id is the primary key (column with unique values) for this table.
The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
banned is an ENUM (category) type of ('Yes', 'No').
The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.
Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03" with at least one trip. Round Cancellation Rate to two decimal points.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Trips table: +----+-----------+-----------+---------+---------------------+------------+ | id | client_id | driver_id | city_id | status | request_at | +----+-----------+-----------+---------+---------------------+------------+ | 1 | 1 | 10 | 1 | completed | 2013-10-01 | | 2 | 2 | 11 | 1 | cancelled_by_driver | 2013-10-01 | | 3 | 3 | 12 | 6 | completed | 2013-10-01 | | 4 | 4 | 13 | 6 | cancelled_by_client | 2013-10-01 | | 5 | 1 | 10 | 1 | completed | 2013-10-02 | | 6 | 2 | 11 | 6 | completed | 2013-10-02 | | 7 | 3 | 12 | 6 | completed | 2013-10-02 | | 8 | 2 | 12 | 12 | completed | 2013-10-03 | | 9 | 3 | 10 | 12 | completed | 2013-10-03 | | 10 | 4 | 13 | 12 | cancelled_by_driver | 2013-10-03 | +----+-----------+-----------+---------+---------------------+------------+ Users table: +----------+--------+--------+ | users_id | banned | role | +----------+--------+--------+ | 1 | No | client | | 2 | Yes | client | | 3 | No | client | | 4 | No | client | | 10 | No | driver | | 11 | No | driver | | 12 | No | driver | | 13 | No | driver | +----------+--------+--------+ Output: +------------+-------------------+ | Day | Cancellation Rate | +------------+-------------------+ | 2013-10-01 | 0.33 | | 2013-10-02 | 0.00 | | 2013-10-03 | 0.50 | +------------+-------------------+ Explanation: On 2013-10-01: - There were 4 requests in total, 2 of which were canceled. - However, the request with Id=2 was made by a banned client (User_Id=2), so it is ignored in the calculation. - Hence there are 3 unbanned requests in total, 1 of which was canceled. - The Cancellation Rate is (1 / 3) = 0.33 On 2013-10-02: - There were 3 requests in total, 0 of which were canceled. - The request with Id=6 was made by a banned client, so it is ignored. - Hence there are 2 unbanned requests in total, 0 of which were canceled. - The Cancellation Rate is (0 / 2) = 0.00 On 2013-10-03: - There were 3 requests in total, 1 of which was canceled. - The request with Id=8 was made by a banned client, so it is ignored. - Hence there are 2 unbanned request in total, 1 of which were canceled. - The Cancellation Rate is (1 / 2) = 0.50
Problem summary: 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. 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'). Table: Users +-------------+----------+ | Column Name | Type | +-------------+----------+ | users_id | int | | banned | enum | | role | enum | +-------------+----------+ users_id is the primary key (column with unique values) for this table. The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers": {"Trips": ["id", "client_id", "driver_id", "city_id", "status", "request_at"], "Users": ["users_id", "banned", "role"]}, "rows": {"Trips": [["1", "1", "10", "1", "completed", "2013-10-01"], ["2", "2", "11", "1", "cancelled_by_driver", "2013-10-01"], ["3", "3", "12", "6", "completed", "2013-10-01"], ["4", "4", "13", "6", "cancelled_by_client", "2013-10-01"], ["5", "1", "10", "1", "completed", "2013-10-02"], ["6", "2", "11", "6", "completed", "2013-10-02"], ["7", "3", "12", "6", "completed", "2013-10-02"], ["8", "2", "12", "12", "completed", "2013-10-03"], ["9", "3", "10", "12", "completed", "2013-10-03"], ["10", "4", "13", "12", "cancelled_by_driver", "2013-10-03"]], "Users": [["1", "No", "client"], ["2", "Yes", "client"], ["3", "No", "client"], ["4", "No", "client"], ["10", "No", "driver"], ["11", "No", "driver"], ["12", "No", "driver"], ["13", "No", "driver"]]}}hopper-company-queries-i)hopper-company-queries-ii)hopper-company-queries-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #262: Trips and Users
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #262: Trips and Users
// 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"]]
// Accepted solution for LeetCode #262: Trips and Users
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #262: Trips and Users
// 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"]]
# Accepted solution for LeetCode #262: Trips and Users
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"]]
// Accepted solution for LeetCode #262: Trips and Users
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #262: Trips and Users
// 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"]]
// Accepted solution for LeetCode #262: Trips and Users
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #262: Trips and Users
// 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"]]
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.