How to Design a Parking Lot - Machine Coding Interview
Designing a parking lot is a popular machine coding interview question which is asked in many companies like Swiggy/Flipkart/Vymo and many more. We will design a parking lot using Object Oriented Design principles.
Designing a parking lot is a popular machine coding interview question which is asked in many companies like Swiggy/Flipkart/Vymo and many more. I recently had an experience where I was asked the same question, so this article is backed by recent experience.
In any machine coding interview, we should try to have some structure to the interview. This helps keep the interviewer and you on the same page. In this article too, we will follow a similar structure. You can read more about the machine coding interview structure in another article - How to crack your machine coding interview?
Also read: System Design Interview Cheatsheet
We need to create a tool for parking lot management. This will be a CLI application which will be used by the parking lot administrator. A vehicle can be parked for any number of hours and this parking lot has a fixed capacity of parking spots. Also, we need to provide support for three main types of vehicles - Cars, Bikes and Trucks.
Now, we have the problem statement with us. Let’s try to reduce it to the actionable requirements. We will try to come up with a few requirements and break them down into smaller action items.
- Ability to create a parking lot.
- Add floors to the parking lot.
- Each floor will have a fixed number of parking spots.
- While creating a floor, we should be able to define how many spots are there for a particular vehicle.
- For the sake of simplicity, we will keep all the bike spots first, followed by cars and then trucks.
- We should be able to park a vehicle in the first available parking spot for that vehicle type. A ticket should be returned when successfully parked.
- Ability to remove a vehicle from a given parking spot id.
- Parking ticket should have the following fields - Time at which the vehicle was parked, floor and spot id.
- Ability to display the number of free spots of each type on each floor.
- Display all cars of a particular color.
These are some of the requirements with us. You can extend this to have some additional requirements to make this a bit complex for you while practising.
Now, once we have the detailed requirements with us. Let’s jump to the implementation part. I have explained each of these files listed below in my video on the same topic please check it out here.
I have published the code at our github repository Parking Lot Machine Coding Solution.