Car rental utilization by city (SQL)
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Dec 2025. It tests sql, aggregation and data engineering.
A car rental company keeps its fleet and its rentals in four tables. For one day D, write SQL that reports, for each city and car size, how many cars were rented that day, how many cars are in the fleet, and the utilization rate.
Interface
Unless you agree on a different interface with the interviewer, assume these tables:
user(user_id)location(location_id, city)-
car(car_id, car_size, location_id, is_active)—car_sizeis a value such ascompact,midsizeorsuv.location_idis the car's home location.is_active = 1means the car is in the fleet. -
fct_rental(rental_id, car_id, pickup_location_id, pickup_ts, dropoff_ts)—dropoff_tsisNULLwhile the car is still out.
Return one row per (city, car_size) with:
-
rented_cars— distinct cars with a rental that overlaps dayD. A rental overlapsDwhen it starts before the end ofDand ends after the start ofD. ANULLdrop-off counts as still ongoing. -
inventory_cars— active cars of that size whose home location is in that city. -
utilization_rate—rented_cars / inventory_carsas a decimal, orNULLwhen the inventory is zero.
Attribute each rental to the city of the car's home location, so the numerator and the denominator count the same cars.
Example
D = '2025-01-15'. Locations: 1 = Austin, 2 = Denver. Cars: c1, c2 (Austin, compact), c3 (Austin, suv), c4 (Denver, compact), c5 (Denver, compact, inactive).
rental car pickup_ts dropoff_ts
r1 c1 2025-01-14 10:00 2025-01-15 09:00
r2 c1 2025-01-15 12:00 2025-01-15 18:00
r3 c3 2025-01-15 22:00 NULL
r4 c2 2025-01-13 08:00 2025-01-15 00:00 # ends exactly at the start of D: no overlap
r5 c4 2025-01-16 08:00 2025-01-17 08:00 # starts after D
city car_size rented_cars inventory_cars utilization_rate
Austin compact 1 2 0.50 # c1 counted once
Austin suv 1 1 1.00
Denver compact 0 1 0.00 # c5 is inactive
Clarifying questions
- Should a rental count toward the pickup city or the car's home city? What if a car was moved between cities?
- Should size groups with no active cars appear at all?
- Are timestamps in one time zone? Which one defines "day
D"? - Do inactive cars that were still rented that day count as rented?
What a strong answer covers
- Two aggregates, inventory and rented, built separately and then joined. Joining rentals to cars before counting inventory would inflate the counts.
- The overlap condition written with half-open bounds:
pickup_ts < D + 1 day AND (dropoff_ts IS NULL OR dropoff_ts > D). -
COUNT(DISTINCT car_id), so a car rented twice in one day counts once. - A
LEFT JOINfrom inventory, so groups with zero rentals show0.NULLIForCASEto avoid division by zero, and decimal division instead of integer division.
Follow-ups
- Report utilization for every day of a month in one query.
- Measure utilization by hours rented instead of by cars touched.
- Cars move between cities. How would you model each car's location over time?
Where it's asked
- Meta · AI-enabled coding · Last asked Dec 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
sql · aggregation · data engineering
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
