LeetCode #3601 — MEDIUM

Find Drivers with Improved Fuel Efficiency

Move from brute-force thinking to an efficient approach using core interview patterns strategy.

Solve on LeetCode
The Problem

Problem Statement

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
  • First half: January to June, Second half: July to December
  • Only include drivers who have trips in both halves of the year
  • Calculate the efficiency improvement as (second_half_avg - first_half_avg)
  • Round all results to 2 decimal places

Return 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:

  • Alice Johnson (driver_id = 1):
    • First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)
    • First half average efficiency: (11.81 + 12.12) / 2 = 11.97
    • Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)
    • Second half average efficiency: (13.64 + 14.40) / 2 = 14.02
    • Efficiency improvement: 14.02 - 11.97 = 2.05
  • Bob Smith (driver_id = 2):
    • First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)
    • First half average efficiency: (11.11 + 11.36) / 2 = 11.24
    • Second half trips: Oct 5 (200.0/15.0 = 13.33)
    • Second half average efficiency: 13.33
    • Efficiency improvement: 13.33 - 11.24 = 2.10 (rounded to 2 decimal places)
  • Drivers not included:
    • Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)
    • David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)
    • Emma Brown (driver_id = 5): Only has trips in first half (Feb)

The output table is ordered by efficiency improvement in descending order then by name in ascending order.

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

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

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: General problem-solving

Example 1

{"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]]}}
Step 02

Core Insight

What unlocks the optimal approach

  • No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

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",
//         ]
//     ]
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(n)
Space
O(1)

Approach Breakdown

BRUTE FORCE
O(n²) time
O(1) space

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.

OPTIMIZED
O(n) time
O(1) space

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.

Shortcut: If you are using nested loops on an array, there is almost always an O(n) solution. Look for the right auxiliary state.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

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.