In this post I will discuss how to approach any design problem and we will design a parking lot for an example. When you are trying to approach a design problem, try to check out the nouns.
For the parking lot, we have the following nouns.
Parking Lot, Parking Space, Car
There can be also different types of parking spaces like - VIP Parking Space, Restricted Parking Space, Reserved Parking Space etc.
There can also be different types of Parking lots. Let's say Basement Parking Lot which is opened for a limited hours of the day.
Now let's take out the crucial points.
1) Parking lot has Parking Space.
2) Parking Space is either full or empty
3) There should be a Parking Lot manager which manages the parking lots. The user makes a request to the Parking lot manager requesting a parking space in a specific parking lot and the Parking Lot manager handles the request.
So, we have more or less the below interfaces.
- getParkingSpace(IParkingLot parkingLot,String carNo)
- freeParkingSpace(String carNo)
and below classes
- List<ParkingSpace> parkingSpaces
- boolean isFull
- String opensAt
- String closesAt
- boolean isOccupied
- List<ParkingLot> parkingLots;
- Map<String,IParkingSpace> carNoToParkingSpaceMap
This is the basic skeleton of our design. We can now add many more things to the above design. We can have different parking lots which implements our IParkingLot interface.
For example we can have a PrivilegedParkingLot for which you need specific privilege to park your car there. We can add one more attribute "isPrivileged" to the to our PrivilegedParkingLot class and do some more verification when we override the getParkingSpace() method to check if the specific car has the privilege to park there.
This is more than enough to impress any interviewer. Once your basic model is ready, you can add any number of features you want to add. The interviewer is interested in Object orientedness of your design.
You can use this approach in any design problem (For example - Design Snake and Ladder game) that you have. Just check out the physical objects and there would be a virtual object like the ParkingLotManager in this case.
Just to give some pointers for the Snake and Ladder game design, here are the Objects that you have more or less.
Snake, Ladder, Board, Square, Players, Dice and the Virtual object Game. Just use the same approach I have discussed above and try to apply that to this design problem.
Sharing is Caring!
RECOMMENDED POSTS FOR YOU