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:
Drivers - Contains driver information including accidents
Vehicles - Maps drivers to vehicles with fuel types
Trips - Contains trip data with ratings and distances

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.

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
Electric Division👑Driver 2Gasoline Division👑Driver 3Ranking Criteria1. Average Rating ↓2. Total Distance ↓3. Accidents ↑RANK() OVER (PARTITION BY fuel_type ORDER BY ...)
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

n
2n
Linearithmic
Space Complexity
O(n)

Space for intermediate grouped results and ranking

n
2n
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
Asked in
Uber 45 Lyft 30 Amazon 25 Google 20
28.4K Views
Medium-High Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen