← Questions

Car rental utilization by city (SQL)

0
MetaCodingLast asked Dec 2025

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_size is a value such as compact, midsize or suv. location_id is the car's home location. is_active = 1 means the car is in the fleet.
  • fct_rental(rental_id, car_id, pickup_location_id, pickup_ts, dropoff_ts) — dropoff_ts is NULL while the car is still out.

Return one row per (city, car_size) with:

  • rented_cars — distinct cars with a rental that overlaps day D. A rental overlaps D when it starts before the end of D and ends after the start of D. A NULL drop-off counts as still ongoing.
  • inventory_cars — active cars of that size whose home location is in that city.
  • utilization_rate — rented_cars / inventory_cars as a decimal, or NULL when 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 JOIN from inventory, so groups with zero rentals show 0. NULLIF or CASE to 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding