Least Efficient Driver by Completed Deliveries per Hour

Quick Overview

A SQL problem that ranks delivery drivers by completed deliveries per hour driven over a fixed date range and returns the least efficient one. It tests aggregating two activity tables without double counting, keeping drivers who completed nothing, half-open date filters, rounding and a tie-breaking rule.

Least Efficient Driver by Completed Deliveries per Hour

Company: Instacart

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Find the least efficient driver between `'2024-01-01'` (inclusive) and `'2024-03-02'` (exclusive). A driver's efficiency is `deliveries_completed / total_hours_driven` for that period, and the least efficient driver has the lowest ratio. Completed deliveries have `status = 'completed'`. Return `driver_id`, `driver_name`, `deliveries_completed`, `total_hours_driven` and `efficiency_ratio` for that driver. ```hint Count the rows Check how many rows a driver with several log entries and several deliveries produces after your joins. ``` ### Input Table(s) `drivers` has one row per driver. | Column | Type | Meaning | |---|---|---| | driver_id | INT | Driver identifier (primary key) | | driver_name | VARCHAR | Driver's name | `deliveries` has one row per delivery assigned to a driver. | Column | Type | Meaning | |---|---|---| | delivery_id | INT | Delivery identifier (primary key) | | driver_id | INT | Driver assigned to the delivery (references `drivers`) | | delivery_date | DATE | Date the delivery belongs to | | status | VARCHAR | One of `'completed'`, `'failed'`, `'pending'` | `driver_logs` has one row per logged driving shift. | Column | Type | Meaning | |---|---|---| | log_id | INT | Log identifier (primary key) | | driver_id | INT | Driver who logged the hours (references `drivers`) | | log_date | DATE | Date of the shift | | hours_driven | DECIMAL(5,2) | Hours driven in the shift, always positive | ### Output Contract - Columns in this order: `driver_id`, `driver_name`, `deliveries_completed`, `total_hours_driven`, `efficiency_ratio`. - `deliveries_completed` is the number of the driver's deliveries with `status = 'completed'` and a `delivery_date` in the period (0 when there are none). - `total_hours_driven` is the sum of `hours_driven` over the driver's logs with a `log_date` in the period, rounded to 2 decimals. - `efficiency_ratio` is `deliveries_completed` divided by the unrounded hour total, rounded to 4 decimals. - Return exactly one row: the driver with the lowest `efficiency_ratio` (compared after rounding to 4 decimals). Ties on ratio go to the smallest `driver_id`. ### Example Input `drivers`: | driver_id | driver_name | |---|---| | 1 | Ava | | 2 | Ben | | 3 | Cara | | 4 | Dev | Input `driver_logs`: | log_id | driver_id | log_date | hours_driven | |---|---|---|---| | 1 | 1 | 2024-01-05 | 4.50 | | 2 | 1 | 2024-02-10 | 5.50 | | 3 | 2 | 2024-01-20 | 5.00 | | 4 | 3 | 2024-03-01 | 2.25 | | 5 | 3 | 2024-03-02 | 6.00 | | 6 | 4 | 2023-12-31 | 8.00 | Input `deliveries`: | delivery_id | driver_id | delivery_date | status | |---|---|---|---| | 101 | 1 | 2024-01-05 | completed | | 102 | 1 | 2024-02-10 | completed | | 103 | 1 | 2024-02-10 | failed | | 104 | 2 | 2024-01-20 | completed | | 105 | 3 | 2024-03-01 | pending | | 106 | 3 | 2024-02-15 | failed | | 107 | 4 | 2024-02-01 | completed | Expected output: | driver_id | driver_name | deliveries_completed | total_hours_driven | efficiency_ratio | |---|---|---|---|---| | 3 | Cara | 0 | 2.25 | 0.0000 | Ava has 2 completed deliveries in 10.00 hours and Ben has 1 in 5.00 hours, so both sit at 0.2000. Cara's only log in the period is the 2.25-hour shift on 2024-03-01 (the 2024-03-02 shift falls on the excluded end date), and she has no completed deliveries, so her ratio is 0. Dev completed a delivery but logged no hours in the period, so he is not considered. ### Constraints and Clarifications - The same period applies to both tables: dates on or after 2024-01-01 and before 2024-03-02. March 1 is included and March 2 is not. - Only drivers with logged hours greater than zero in the period are considered. Because `hours_driven` is always positive, that means at least one log in the period. At least one driver qualifies. - A driver with logged hours and no completed deliveries is a valid candidate with a ratio of 0. - Only `'completed'` deliveries count. `'failed'` and `'pending'` deliveries are ignored. - A driver can have several logs and several deliveries on the same date. - No column contains NULL. - `drivers` has at most 10,000 rows; `deliveries` and `driver_logs` have at most 100,000 rows each.

Overview: A SQL problem that ranks delivery drivers by completed deliveries per hour driven over a fixed date range and returns the least efficient one. It tests aggregating two activity tables without double counting, keeping drivers who completed nothing, half-open date filters, rounding and a tie-breaking rule.

Instacart logo
Instacart
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Find the least efficient driver between '2024-01-01' (inclusive) and '2024-03-02' (exclusive). A driver's efficiency is deliveries_completed / total_hours_driven for that period, and the least efficient driver has the lowest ratio. Completed deliveries have status = 'completed'. Return driver_id, driver_name, deliveries_completed, total_hours_driven and efficiency_ratio for that driver.

Input Table(s)

drivers has one row per driver.

ColumnTypeMeaning
driver_idINTDriver identifier (primary key)
driver_nameVARCHARDriver's name

deliveries has one row per delivery assigned to a driver.

ColumnTypeMeaning
delivery_idINTDelivery identifier (primary key)
driver_idINTDriver assigned to the delivery (references drivers)
delivery_dateDATEDate the delivery belongs to
statusVARCHAROne of 'completed', 'failed', 'pending'

driver_logs has one row per logged driving shift.

ColumnTypeMeaning
log_idINTLog identifier (primary key)
driver_idINTDriver who logged the hours (references drivers)
log_dateDATEDate of the shift
hours_drivenDECIMAL(5,2)Hours driven in the shift, always positive

Output Contract

  • Columns in this order: driver_id , driver_name , deliveries_completed , total_hours_driven , efficiency_ratio .
  • deliveries_completed is the number of the driver's deliveries with status = 'completed' and a delivery_date in the period (0 when there are none).
  • total_hours_driven is the sum of hours_driven over the driver's logs with a log_date in the period, rounded to 2 decimals.
  • efficiency_ratio is deliveries_completed divided by the unrounded hour total, rounded to 4 decimals.
  • Return exactly one row: the driver with the lowest efficiency_ratio (compared after rounding to 4 decimals). Ties on ratio go to the smallest driver_id .

Example

Input drivers:

driver_iddriver_name
1Ava
2Ben
3Cara
4Dev

Input driver_logs:

log_iddriver_idlog_datehours_driven
112024-01-054.50
212024-02-105.50
322024-01-205.00
432024-03-012.25
532024-03-026.00
642023-12-318.00

Input deliveries:

delivery_iddriver_iddelivery_datestatus
10112024-01-05completed
10212024-02-10completed
10312024-02-10failed
10422024-01-20completed
10532024-03-01pending
10632024-02-15failed
10742024-02-01completed

Expected output:

driver_iddriver_namedeliveries_completedtotal_hours_drivenefficiency_ratio
3Cara02.250.0000

Ava has 2 completed deliveries in 10.00 hours and Ben has 1 in 5.00 hours, so both sit at 0.2000. Cara's only log in the period is the 2.25-hour shift on 2024-03-01 (the 2024-03-02 shift falls on the excluded end date), and she has no completed deliveries, so her ratio is 0. Dev completed a delivery but logged no hours in the period, so he is not considered.

Constraints and Clarifications

  • The same period applies to both tables: dates on or after 2024-01-01 and before 2024-03-02. March 1 is included and March 2 is not.
  • Only drivers with logged hours greater than zero in the period are considered. Because hours_driven is always positive, that means at least one log in the period. At least one driver qualifies.
  • A driver with logged hours and no completed deliveries is a valid candidate with a ratio of 0.
  • Only 'completed' deliveries count. 'failed' and 'pending' deliveries are ignored.
  • A driver can have several logs and several deliveries on the same date.
  • No column contains NULL.
  • drivers has at most 10,000 rows; deliveries and driver_logs have at most 100,000 rows each.