KatInterview

Top 5 Delivery Routes by Fuel Efficiency

SQL & DataAuto-graded · 21 test cases
MediumAuto-gradedSQL & Dataaggregationfilteringsorting

Last updated

Tests aggregating trip data across two tables, guarding against division by zero, and ranking results with a deterministic tie-break.

Problem

You manage a logistics database tracking delivery runs across a fleet of vehicles. Your data lives in two collections: trips and service_records.

Data Shapes

trips — one entry per delivery run:

  • trip_id: unique identifier for the run
  • vehicle_id: which vehicle made the run
  • distance_miles: kilometres driven (non-negative)
  • fuel_consumed_gallons: fuel used (non-negative)

service_records — one entry per service event:

  • log_id: unique identifier for the service event
  • vehicle_id: which vehicle was serviced
  • maintenance_cost: cost of the service event

Task

Return the 5 most fuel-efficient vehicles that have at least one entry in service_records. Efficiency for a vehicle is defined as:

efficiency = total distance driven / total fuel consumed

where both totals are summed across all of that vehicle's trips.

Rules

  1. A vehicle is eligible only if its vehicle_id appears at least once in service_records, regardless of cost.
  2. If a vehicle's total fuel consumption is 0 or absent, its efficiency is 0 (do not divide by zero).
  3. An eligible vehicle with no trips at all still appears in the output with efficiency 0.
  4. Return each eligible vehicle as a dict with keys vehicle_id and efficiency.
  5. Sort results by efficiency descending. Break ties by vehicle_id ascending.
  6. Return at most 5 results; return fewer if fewer than 5 eligible vehicles exist.

Constraints

  • 0 ≤ distance_miles, 0 ≤ fuel_consumed_gallons for every trip row.
  • Either field may be None; treat None as 0.
  • A vehicle may have multiple service records; it still counts as one eligible vehicle.
  • Do not average per-trip ratios. Compute the ratio of the summed totals.

Examples

Example 1

Input: top_5_efficient_routes([{'trip_id': 1, 'vehicle_id': 'A1', 'distance_miles': 100, 'fuel_consumed_gallons': 5}, {'trip_id': 2, 'vehicle_id': 'A1', 'distance_miles': 50, 'fuel_consumed_gallons': 5}, {'trip_id': 3, 'vehicle_id': 'B2', 'distance_miles': 200, 'fuel_consumed_gallons': 0}], [{'log_id': 101, 'vehicle_id': 'A1', 'maintenance_cost': 500}])

Output: [{'vehicle_id': 'A1', 'efficiency': 15}]

Hints

Hint 1

Build the set of eligible vehicles from service_records first, then look up trip totals for each one — not the other way around. This ensures vehicles with service records but no trips still appear in your output.

Hint 2

When computing efficiency, guard the division at the aggregate level: check whether total fuel is zero before dividing the total distance by it. Filtering out individual zero-fuel trips changes the numerator and gives the wrong answer.

Hint 3

Your sort key needs two levels: primary on efficiency (descending) and secondary on vehicle_id (ascending). Without the tie-break, results for vehicles with equal efficiency are non-deterministic.