Find Top Performing Driver - Problem
Find the Top Performing Driver
You're working as a data analyst for Uber and need to identify the best driver for each fuel type based on their trip performance. This involves analyzing driver ratings, total distance traveled, and accident history across multiple database tables.
Given three tables:
•
•
•
Performance Ranking Criteria:
1. Primary: Highest average rating (rounded to 2 decimal places)
2. Tiebreaker 1: Longest total distance traveled
3. Tiebreaker 2: Fewest accidents
Return one top driver per fuel type, ordered by fuel type alphabetically.
You're working as a data analyst for Uber and need to identify the best driver for each fuel type based on their trip performance. This involves analyzing driver ratings, total distance traveled, and accident history across multiple database tables.
Given three tables:
•
Drivers - Contains driver information including accidents•
Vehicles - Maps drivers to vehicles with fuel types•
Trips - Contains trip data with ratings and distancesPerformance Ranking Criteria:
1. Primary: Highest average rating (rounded to 2 decimal places)
2. Tiebreaker 1: Longest total distance traveled
3. Tiebreaker 2: Fewest accidents
Return one top driver per fuel type, ordered by fuel type alphabetically.
Input & Output
example_1.sql — Basic Example
$
Input:
Drivers: [(1,'Alice',34,10,1), (2,'Bob',45,20,3), (3,'Charlie',28,5,0)]\nVehicles: [(100,1,'Sedan','Gasoline',20000), (101,2,'SUV','Electric',30000), (102,3,'Coupe','Gasoline',15000)]\nTrips: [(201,100,50,30,5), (202,100,30,20,4), (203,101,100,60,4), (204,101,80,50,5), (205,102,40,30,5), (206,102,60,40,5)]
›
Output:
[('Electric',2,4.50,180), ('Gasoline',3,5.00,100)]
💡 Note:
For Electric: Driver 2 (Bob) has avg rating 4.50 with 180 total distance. For Gasoline: Driver 3 (Charlie) beats Driver 1 (Alice) with 5.00 vs 4.50 avg rating.
example_2.sql — Tie Breaking by Distance
$
Input:
Same drivers, but Alice and Charlie both have 4.50 rating, Charlie has more distance
›
Output:
[('Gasoline',3,4.50,120)]
💡 Note:
When ratings are tied, the driver with longer total distance wins the tiebreaker.
example_3.sql — Tie Breaking by Accidents
$
Input:
Same drivers with same rating and distance, but different accident counts
›
Output:
[('Gasoline',3,4.50,100)]
💡 Note:
When rating and distance are tied, the driver with fewer accidents wins (Charlie has 0 vs Alice's 1).
Visualization
Tap to expand
Understanding the Visualization
1
Data Integration
JOIN driver, vehicle, and trip data to create complete performance records
2
Performance Metrics
Calculate average rating and total distance for each driver per fuel type
3
Ranking Within Categories
Use RANK() OVER PARTITION BY fuel_type to rank drivers within each category
4
Champion Selection
Select the #1 ranked driver from each fuel type category
Key Takeaway
🎯 Key Insight: Window functions with PARTITION BY allow efficient ranking within categories, eliminating the need for complex subqueries and manual tie-breaking logic.
Time & Space Complexity
Time Complexity
O(n log n)
Single scan with sorting for ranking within each partition
⚡ Linearithmic
Space Complexity
O(n)
Space for intermediate grouped results and ranking
⚡ Linearithmic Space
Constraints
- 1 ≤ drivers.length ≤ 103
- 1 ≤ vehicles.length ≤ 103
- 1 ≤ trips.length ≤ 104
- 1 ≤ rating ≤ 5
- 0 ≤ accidents ≤ 100
- Each driver has at least one trip
- Average ratings should be rounded to exactly 2 decimal places
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code