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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: drivers
+-------------+---------+ | Column Name | Type | +-------------+---------+ | driver_id | int | | driver_name | varchar | +-------------+---------+ driver_id is the unique identifier for this table. Each row contains information about a driver.
Table: trips
+---------------+---------+ | Column Name | Type | +---------------+---------+ | trip_id | int | | driver_id | int | | trip_date | date | | distance_km | decimal | | fuel_consumed | decimal | +---------------+---------+ trip_id is the unique identifier for this table. Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
Write a solution to find drivers whose fuel efficiency has improved by comparing their average fuel efficiency in the first half of the year with the second half of the year.
distance_km / fuel_consumed for each tripsecond_half_avg - first_half_avg)2 decimal placesReturn the result table ordered by efficiency improvement in descending order, then by driver name in ascending order.
The result format is in the following example.
Example:
Input:
drivers table:
+-----------+---------------+ | driver_id | driver_name | +-----------+---------------+ | 1 | Alice Johnson | | 2 | Bob Smith | | 3 | Carol Davis | | 4 | David Wilson | | 5 | Emma Brown | +-----------+---------------+
trips table:
+---------+-----------+------------+-------------+---------------+ | trip_id | driver_id | trip_date | distance_km | fuel_consumed | +---------+-----------+------------+-------------+---------------+ | 1 | 1 | 2023-02-15 | 120.5 | 10.2 | | 2 | 1 | 2023-03-20 | 200.0 | 16.5 | | 3 | 1 | 2023-08-10 | 150.0 | 11.0 | | 4 | 1 | 2023-09-25 | 180.0 | 12.5 | | 5 | 2 | 2023-01-10 | 100.0 | 9.0 | | 6 | 2 | 2023-04-15 | 250.0 | 22.0 | | 7 | 2 | 2023-10-05 | 200.0 | 15.0 | | 8 | 3 | 2023-03-12 | 80.0 | 8.5 | | 9 | 3 | 2023-05-18 | 90.0 | 9.2 | | 10 | 4 | 2023-07-22 | 160.0 | 12.8 | | 11 | 4 | 2023-11-30 | 140.0 | 11.0 | | 12 | 5 | 2023-02-28 | 110.0 | 11.5 | +---------+-----------+------------+-------------+---------------+
Output:
+-----------+---------------+------------------+-------------------+------------------------+ | driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement | +-----------+---------------+------------------+-------------------+------------------------+ | 2 | Bob Smith | 11.24 | 13.33 | 2.10 | | 1 | Alice Johnson | 11.97 | 14.02 | 2.05 | +-----------+---------------+------------------+-------------------+------------------------+
Explanation:
The output table is ordered by efficiency improvement in descending order then by name in ascending order.
Problem summary: Table: drivers +-------------+---------+ | Column Name | Type | +-------------+---------+ | driver_id | int | | driver_name | varchar | +-------------+---------+ driver_id is the unique identifier for this table. Each row contains information about a driver. Table: trips +---------------+---------+ | Column Name | Type | +---------------+---------+ | trip_id | int | | driver_id | int | | trip_date | date | | distance_km | decimal | | fuel_consumed | decimal | +---------------+---------+ trip_id is the unique identifier for this table. Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip. Write a solution to find drivers whose fuel efficiency has improved by comparing their average fuel efficiency in the first half of the year with the second half of the year. Calculate fuel efficiency as distance_km / fuel_consumed for each trip
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"drivers":["driver_id","driver_name"],"trips":["trip_id","driver_id","trip_date","distance_km","fuel_consumed"]},"rows":{"drivers":[[1,"Alice Johnson"],[2,"Bob Smith"],[3,"Carol Davis"],[4,"David Wilson"],[5,"Emma Brown"]],"trips":[[1,1,"2023-02-15",120.5,10.2],[2,1,"2023-03-20",200.0,16.5],[3,1,"2023-08-10",150.0,11.0],[4,1,"2023-09-25",180.0,12.5],[5,2,"2023-01-10",100.0,9.0],[6,2,"2023-04-15",250.0,22.0],[7,2,"2023-10-05",200.0,15.0],[8,3,"2023-03-12",80.0,8.5],[9,3,"2023-05-18",90.0,9.2],[10,4,"2023-07-22",160.0,12.8],[11,4,"2023-11-30",140.0,11.0],[12,5,"2023-02-28",110.0,11.5]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// import pandas as pd
//
//
// def find_improved_efficiency_drivers(
// drivers: pd.DataFrame, trips: pd.DataFrame
// ) -> pd.DataFrame:
// trips = trips.copy()
// trips["trip_date"] = pd.to_datetime(trips["trip_date"])
// trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
// trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
// half_avg = (
// trips.groupby(["driver_id", "half"])["efficiency"]
// .mean()
// .reset_index(name="half_avg")
// )
// pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
// columns={1: "first_half_avg", 2: "second_half_avg"}
// )
// pivot = pivot.dropna()
// pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
// pivot["efficiency_improvement"] = (
// pivot["second_half_avg"] - pivot["first_half_avg"]
// ).round(2)
// pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
// pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
// result = pivot.reset_index().merge(drivers, on="driver_id")
// result = result.sort_values(
// by=["efficiency_improvement", "driver_name"], ascending=[False, True]
// )
// return result[
// [
// "driver_id",
// "driver_name",
// "first_half_avg",
// "second_half_avg",
// "efficiency_improvement",
// ]
// ]
// Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// import pandas as pd
//
//
// def find_improved_efficiency_drivers(
// drivers: pd.DataFrame, trips: pd.DataFrame
// ) -> pd.DataFrame:
// trips = trips.copy()
// trips["trip_date"] = pd.to_datetime(trips["trip_date"])
// trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
// trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
// half_avg = (
// trips.groupby(["driver_id", "half"])["efficiency"]
// .mean()
// .reset_index(name="half_avg")
// )
// pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
// columns={1: "first_half_avg", 2: "second_half_avg"}
// )
// pivot = pivot.dropna()
// pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
// pivot["efficiency_improvement"] = (
// pivot["second_half_avg"] - pivot["first_half_avg"]
// ).round(2)
// pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
// pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
// result = pivot.reset_index().merge(drivers, on="driver_id")
// result = result.sort_values(
// by=["efficiency_improvement", "driver_name"], ascending=[False, True]
// )
// return result[
// [
// "driver_id",
// "driver_name",
// "first_half_avg",
// "second_half_avg",
// "efficiency_improvement",
// ]
// ]
# Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
import pandas as pd
def find_improved_efficiency_drivers(
drivers: pd.DataFrame, trips: pd.DataFrame
) -> pd.DataFrame:
trips = trips.copy()
trips["trip_date"] = pd.to_datetime(trips["trip_date"])
trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
half_avg = (
trips.groupby(["driver_id", "half"])["efficiency"]
.mean()
.reset_index(name="half_avg")
)
pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
columns={1: "first_half_avg", 2: "second_half_avg"}
)
pivot = pivot.dropna()
pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
pivot["efficiency_improvement"] = (
pivot["second_half_avg"] - pivot["first_half_avg"]
).round(2)
pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
result = pivot.reset_index().merge(drivers, on="driver_id")
result = result.sort_values(
by=["efficiency_improvement", "driver_name"], ascending=[False, True]
)
return result[
[
"driver_id",
"driver_name",
"first_half_avg",
"second_half_avg",
"efficiency_improvement",
]
]
// Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// 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 #3601: Find Drivers with Improved Fuel Efficiency
// import pandas as pd
//
//
// def find_improved_efficiency_drivers(
// drivers: pd.DataFrame, trips: pd.DataFrame
// ) -> pd.DataFrame:
// trips = trips.copy()
// trips["trip_date"] = pd.to_datetime(trips["trip_date"])
// trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
// trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
// half_avg = (
// trips.groupby(["driver_id", "half"])["efficiency"]
// .mean()
// .reset_index(name="half_avg")
// )
// pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
// columns={1: "first_half_avg", 2: "second_half_avg"}
// )
// pivot = pivot.dropna()
// pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
// pivot["efficiency_improvement"] = (
// pivot["second_half_avg"] - pivot["first_half_avg"]
// ).round(2)
// pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
// pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
// result = pivot.reset_index().merge(drivers, on="driver_id")
// result = result.sort_values(
// by=["efficiency_improvement", "driver_name"], ascending=[False, True]
// )
// return result[
// [
// "driver_id",
// "driver_name",
// "first_half_avg",
// "second_half_avg",
// "efficiency_improvement",
// ]
// ]
// Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3601: Find Drivers with Improved Fuel Efficiency
// import pandas as pd
//
//
// def find_improved_efficiency_drivers(
// drivers: pd.DataFrame, trips: pd.DataFrame
// ) -> pd.DataFrame:
// trips = trips.copy()
// trips["trip_date"] = pd.to_datetime(trips["trip_date"])
// trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
// trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
// half_avg = (
// trips.groupby(["driver_id", "half"])["efficiency"]
// .mean()
// .reset_index(name="half_avg")
// )
// pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
// columns={1: "first_half_avg", 2: "second_half_avg"}
// )
// pivot = pivot.dropna()
// pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
// pivot["efficiency_improvement"] = (
// pivot["second_half_avg"] - pivot["first_half_avg"]
// ).round(2)
// pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
// pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
// result = pivot.reset_index().merge(drivers, on="driver_id")
// result = result.sort_values(
// by=["efficiency_improvement", "driver_name"], ascending=[False, True]
// )
// return result[
// [
// "driver_id",
// "driver_name",
// "first_half_avg",
// "second_half_avg",
// "efficiency_improvement",
// ]
// ]
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.