Design a Vehicle Rental System
🚗 What is a vehicle rental system? It is the software behind a car or bike rental desk. A customer books a type of vehicle for a time range, comes to pick it up, and pays when they bring it back. The system has to make sure no vehicle is promised to two customers at the same time.
This problem comes in a few flavours: "design a car rental system", "design a bike rental service", or a rental shop with cars, bikes and vans. They are the same design. A car rental leans on bookings far in advance and long durations, a bike rental leans on short trips and questions like "how many bikes are out right now?". We build one system that handles all of them: vehicle types with their own rates, bookings over time ranges, pricing that depends on the duration, and a fast rented-count query.
Requirements
"Design a vehicle rental system. Customers can reserve a car or a bike, pick it up, and return it."
As usual, the interesting rules are not in the prompt. These questions settle the ones that shape the design.
Clarifying Questions
You: What kinds of vehicles are there, and does the customer pick a specific vehicle?
Interviewer: Bikes, cars and SUVs. The customer books a type, not a specific vehicle. We assign one of that type, and the customer keeps that vehicle for the booking.
You: Do customers only rent right now, or can they book ahead?
Interviewer: They book a time range, possibly in the future. Two bookings on the same vehicle must never overlap. Time is in whole hours, and a range includes its start hour but not its end hour.
You: When several vehicles of the type are free, which one do we give?
Interviewer: The one with the smallest id, so results are predictable.
You: How is the price computed? Is a long rental cheaper per hour?
Interviewer: Yes. Each type has an hourly and a daily rate. Full days cost the daily rate, leftover hours cost the hourly rate but never more than one more day. Returning late costs twice the hourly rate for every late hour. Returning early does not get a refund.
You: What happens if a customer returns late and the vehicle is booked by someone else next?
Interviewer: The next customer cannot pick it up until it is back. Keep it simple: their pick-up just fails until then.
You: What questions does the front desk ask the system?
Interviewer: How many vehicles of a type are out right now, and how many of a type are free for a given range. The first one is checked all the time on the bike side, so it must be cheap.
You: How does the system know the time?
Interviewer: Pick-ups and returns carry a timestamp in hours.
The second answer is the one that changes everything. If customers only rented "right now", each vehicle would need a single is_rented flag. Because they book ranges in the future, each vehicle needs a calendar of bookings, and "is it free?" becomes an interval question. Most of the design work in this problem is in that calendar.
💡 Interview tip: In any booking problem, ask early whether bookings are for "now" or for a future range, and whether the end of a range is included. "Does a booking from 10 to 15 clash with one from 15 to 20?" settles an off-by-one that the tests will certainly check.
Final Requirements
- The fleet has vehicles with an id and a type:
BIKE,CARorSUV. - A customer reserves a type for the hours
[start, end). They get the free vehicle of that type with the smallest id; free means no held booking on it overlaps the range. If none is free, the reservation fails. - A reservation can be cancelled before pick-up, which frees its range.
- A reservation can be picked up from its start hour until just before its end hour, and only if the vehicle is not still out with an earlier customer.
- On return, the vehicle's range is freed at once, and the price is: full days × daily rate + min(leftover hours × hourly rate, daily rate), plus 2 × hourly rate per late hour. Early returns pay the full booking.
- The system answers "how many of this type are out right now?" in constant time, and "how many of this type are free for this range?".
- Pricing rules change often (weekend rates, insurance, discounts), so pricing must be swappable.
Out of scope
- Payments, deposits, insurance and damage checks.
- Several branches and one-way rentals (Extensibility).
- Customer accounts, licences and loyalty.
- Moving a booking to another vehicle when its vehicle is late (Extensibility).