Hopper Company Queries II - Problem
Hopper Company Queries II is a challenging SQL problem that involves calculating the percentage of active drivers for each month in 2020.
You're given three tables:
Your task is to calculate the working percentage for each month, which is defined as:
A driver is considered available in a month if they had already joined the company by that month. A driver is considered working if they accepted at least one ride during that specific month.
Key Requirements:
You're given three tables:
- Drivers: Contains driver information and their join dates
- Rides: Contains all ride requests (both accepted and rejected)
- AcceptedRides: Contains details about rides that were actually completed
Your task is to calculate the working percentage for each month, which is defined as:
(Number of drivers who accepted at least one ride during the month) / (Total number of available drivers during the month) ร 100A driver is considered available in a month if they had already joined the company by that month. A driver is considered working if they accepted at least one ride during that specific month.
Key Requirements:
- Report results for all 12 months of 2020
- Round the working percentage to 2 decimal places
- If no drivers are available in a month, return 0.00%
- Order results by month (1=January, 2=February, etc.)
Input & Output
example_1.sql โ Basic Example
$
Input:
Drivers: [(1,'2019-01-01'), (2,'2019-03-01'), (3,'2020-01-01')]
Rides: [(1,1,'2020-01-15'), (2,2,'2020-02-14'), (3,3,'2020-03-15')]
AcceptedRides: [(1,1,20,30), (3,3,15,25)]
โบ
Output:
[(1, 33.33), (2, 0.00), (3, 50.00), (4, 0.00), ..., (12, 0.00)]
๐ก Note:
January: 3 drivers available (all joined by Jan 31), 1 working (driver 1) โ 1/3 = 33.33%. February: 3 available, 0 working โ 0%. March: 3 available, 1 working (driver 3) โ 33.33%
example_2.sql โ No Available Drivers
$
Input:
Drivers: [(1,'2020-02-01'), (2,'2020-03-01')]
Rides: [(1,1,'2020-01-15')]
AcceptedRides: [(1,1,10,20)]
โบ
Output:
[(1, 0.00), (2, 0.00), (3, 0.00), ..., (12, 0.00)]
๐ก Note:
January: 0 drivers available (none joined yet), so working_percentage = 0.00% by definition. February onwards: drivers become available but may not be working
example_3.sql โ All Drivers Working
$
Input:
Drivers: [(1,'2019-12-01'), (2,'2019-12-01')]
Rides: [(1,1,'2020-06-15'), (2,2,'2020-06-16')]
AcceptedRides: [(1,1,25,35), (2,2,30,40)]
โบ
Output:
[(1, 0.00), (2, 0.00), ..., (6, 100.00), (7, 0.00), ..., (12, 0.00)]
๐ก Note:
June: 2 drivers available (both joined before 2020), 2 working (both accepted rides) โ 2/2 = 100.00%. All other months have 0 working drivers
Visualization
Tap to expand
Understanding the Visualization
1
Driver Timeline
Track when each driver joins the company (availability accumulates)
2
Monthly Activity
For each month, count drivers who accepted at least one ride
3
Calculate Ratios
Working drivers รท Available drivers ร 100 for each month
Key Takeaway
๐ฏ Key Insight: Driver availability is cumulative (once joined, always available), while working status resets each month and requires actual ride activity.
Time & Space Complexity
Time Complexity
O(n log n)
Single pass through data with efficient joins and window functions
โก Linearithmic
Space Complexity
O(n)
Space for intermediate CTE results and final output
โก Linearithmic Space
Constraints
- 1 โค drivers.length โค 104
- 1 โค rides.length โค 104
- 1 โค acceptedRides.length โค rides.length
- All dates are valid and in proper format
- Each driver_id is unique in Drivers table
- Each ride_id is unique in Rides and AcceptedRides tables
- AcceptedRides.ride_id always exists in Rides table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code