Subtitles section Play video
hello viewers today we are going to discuss how to tackle a very commonly asked interview
question about designing parking lot system when an interviewer asked such a question you need to
first understand what he actually wants and what he is looking in the candidate and so you need
to discuss first all the requirements with the interviewer before directly jumping into
designing the parking lot system since the parking lot system design is a very vague question do not
assume anything on your own but it specifically clear out everything with the interviewer doing
the first 5 to 10 minutes of the interview in general you need to understand following when an
interviewer is asking you such question the very first thing he's looking at is how good are you at
requirements collection whether you even clearly list out all the possible requirements and whether
you can successfully scope down the problem into something which you can solve in 30 to 45 minutes
another thing that the interviewer is looking for is your system design it and especially
object oriented design skills he is looking for how well you can design the whole system in terms
of components and classes how will your model parking-lot system into different classes and
objects identify constraints and design interfaces how well versed are you in the object-oriented
design concepts like abstraction encapsulation inheritance and polymorphism can you define the
links and association between the classes and objects in your system another thing that you
need to consider is that when you are designing a parking-lot system you should be discussing the
design in terms of different design patterns the design patterns are well established solution to
the most commonly encountered problems which occurred during software development these
represents best practices used by experienced object-oriented software developers this shows the
interviewer about experience in object oriented analysis and design and provides a positive
feedback to your overall interview performance so the interviewer is looking for whether you
can identify different object-oriented design patterns all programming patterns while designing
the parking-lot system you will be discussing your requirement for the Binod system the interview we
might show some different requirements to you for example he can add two or more entrances or exits
to the parking guard system in case the parking lot system has two or more intense Azure exits the
interviewer is also looking at how you can tackle the concurrency in your design sometimes the
interviewer gave you requirements about selecting a parking spot near some entrance to see how you
can figure out finding the solution for nearest parking spot and the important thing that you
need to understand is that this type of question is not related to designing a distributed system
and so you should not solve it as a distributed system design unless you have clearly discussed
this with the interviewer in the requirement collection phase if you try to solve it as a
distributed system design then the interviewer can interpret it as you have not sufficient experience
to scope down upon them correctly and instead of simplifying you are all complicating the system
design so let's first collect the requirements so the very first requirement is that you should
like this video and you should subscribe to the stand if you haven't done that anyway jokes apart
let's consider that we have a very huge parking lot if you google you will find that there are
many parking lots with parking sizes more than 10,000 for example the word largest parking
lot is in West Edmonton in Alberta Canada which has more than 20,000 parking spots for vehicles
the seattle sea-tac airport has more than 13,000 parking spots for the vehicles so we are talking
about parking spaces in the range of 10,000 to 30,000 so the first requirement is big parking
lots 10k to 30k spots these huge parking lots will have more than one internships and exits in our
case let's assume that our parking lot has four entrances and four exits for influences four exits
the third requirement is the customers collect ticket at the entrances and the parking spot is
assigned on the ticket the fourth requirement is the parking spot assigned to a vehicle should be
near to the entrance so the parking spot should be nearest to the entrance so what it means that if
a customer is entering into the parking lot from this entrance then the parking got assigned should
be the nearest from this entrance and if another customer is actually entering the parking lot from
some other interns like this one then the parking spot assigned to that customer should be nearest
form this interns the fifth comment is the system should not allow more vehicles then the capacity
of the parking lot so there is a limit or capacity and we can assume simply than the capacity that
we have in our parking lot system is let's say 30,000 the sixth requirement is that there will
be different types of parking spot for example there will be parking spots for handicapped they
will be parking spots for compact cars there will be parking spots for large cars etc so in
our design we will consider there are four types of parking spots parking spots and these will be
handicapped parking compact car large car large vehicle compact vehicle and motorcycle the seventh
requirement is the system should support hourly parking rate and the final parking fee should be
based on the total time that a vehicle is spent in the parking lot the customers can pay parking
fee both with cash and credit card so cash and credit cards they are accepted there should be a
monitoring system as well so the monitoring system would be responsible for monitoring how many cars
are entering and exiting etc and of course there could be video surveillance as well as part of
the monitoring system now the last but by the important requirement that most interviewers
actually forget is that you need to design the system in such a way so that it can be applied
for this toleration of parking system for more and more parking lots with minimal changes in
the code and design this means you can take your system and apply it to a different parking lot
maybe for a different shopping mall parking lot or an airport parking lot with minimal changes
mostly due to the configurations as of that new parking lot describing the overall structure of
the parking lot the number of flows the capacity and the different types of parking spots that the
new parking guard system actually support so the basic idea is that you are working for a company
which actually stalls in different parking lot systems and so what you would like to do is you
would like to have your one system designed and then you what you would like to do is that you
just want to apply your system that you have designed and you want to reuse that system for
this tradition of different parking lot systems and the only thing that you would like to now
change is the configuration information this is code the usability now if you check out the online
sources you will find that they discuss so many different superficial requirements but please note
if you discuss those superficial requirements then an experienced interviewer can actually catch you
there for example if there are multiple parking flows let's say in Seattle Airport where we have
like six seven different parking flows then the requirement that there should be a panel at the
internets of each floor showing the number of free parking spots available in that floor is a
conflicting the government with other requirement where we have said that the parking spot will be
assigned to a vehicle at the time of entrance so we don't really need this requirement in our
system why because we already know the parking spot which is assigned to him at the entrance
so if you had a requirement that each parking flow should have a panels showing the available parking
spots then this is a conflicting requirement similarly the other online resources which
discuss some requirements regarding for example the color of the vehicle however an interviewer
can ask you how the parking system will benefit from such requirements how knowing the color of
a car actually hurts indeed overall design of the parking system so you need to understand
this thing that during the Carmen collection you should not discuss some superficial requirements
with the interviewer which are hard for you to justify that why you need them in designing the
parking lot system it also shows that you are not experiencing enough to collect the right set of
requirements and you are over complicating the requirements instead of simplifying the
requirements to come up with a simple design we will design that parking lot system using some
design patterns there are three broad categories of design patterns the first one is traditional
design patterns the creation of design patterns deals with how the objects are instantiated the
second type of design patterns structural design patterns the structural design patterns discuss
how different objects and classes are composed in order to form larger structure and the third
design patterns are behavioral design patterns the behavioral design patterns deals with these
possibilities of the objects and how they interact with each other before going into a design of
the parking lot we are also going to discuss the design approach that we will be taking to design
the parking lot system so there are two different design approaches that people usually take the
first one is top-down design and the second one is the bottom-up design in the top-down design
we first construct the high-level object and then we identify these smaller sub components
in it and then we design those sub components and then in each sub component we identify its
sub components and then we had then design those sub components whereas in a bottom-up approach we
first design the smallest component and then we use those smallest components to design a bigger
component and so on and so on please note that the bottom-up approach is aligned with object oriented
design and so we should be using bottom-up design approach while designing a bug in gaad's system
at this point we should identify different objects and actors in our system so we have
the parking-lot system itself then we have entry and exit terminals entry exit terminals both the
entry and exit terminals will have printers the exit terminal will also have a payment processor
then in our parking lot system we have a parking spot another actor is the ticket itself then we
will have the database we will also have the monitoring system let me know in the comments
below if you think there are other actors as well which we need in our parking lot system I will go
further now into areas of these objects however before going to that I would like to discuss
one other thing if you have seen other online sources you will find that they also describe
the hick 11 actor in the parking lot system and so in their design they usually have a vehicle
class and then they drive different classes from the vehicle class like compact vehicle
large vehicle burst Prakash cetera please note a good interviewer will ask you a question that
why even you need a vehicle glass in your system and why are you defining of a heater class he can
ask you questions like how using a vehicle class helps you in designing the world system what are
the different options or methods that they were heated class need that are helpful for you in
designing your system so please understand the difference between a vehicle parking spot and a
vehicle class the parking lot system will be using the vehicle parking spot but it will not be using
a vehicle class s there is no reason to use a vehicle class along with its subclasses so do
not diss véhicule class along with its subclasses if you don't have a clear story about the usage
in the overall system now let's design different interfaces classes and components in our parking
lot system using bottom-up approach so a vehicle parking spot is an actor or an object in our
system there could be different types of hiding spot example we could have a parking spot for
handicapped we could have a compact parking spot we could have a large parking spot we could have
motorcycle parking spot as we have discussed in the requirements now there are two different ways
to represent those different types of parking spots one way is to use in M types to represent
each type of parking spot however this shows that you are not that proficient in the object oriented
design as this approach has some flaws for example if you need to add another parking spot type then
it could require changes at various places in the code which would violate the open/close design
principle this principle dictates that existing and well tested classes should not be modified
whether new feature needs to be built the second approach which is more preferable approach is that
we actually drive each different types of parking spot from apparent parking spot interface or class
so this diagram shows all the different types of parking spot available in our system in terms of
different classes so we have a parent parking spot class and which has an ID and deserve as
a property and there will be methods to get the property like gate ID and there would be a gate
inside function on the reserved property as well and now if you see we have handicapped parking
spot compact parking spot large parking spot and motorcycle five spots are different classes which
are derived from this parking spot and now in that case if we have to add another type of parking
spot what we would need to do is just actually exchange this parking spot class by some other
parking spot class that's it please note that the parking spot class will be an extra class what it
means that we cannot instantiate an object of a parking spot we could instantiate an object
of handicapped parking spot or large or compact parking spot a motorcycle parking spot but not
the parent parking spot class object we will have parking ticket where we will have parking ticket
ID in it parking spot ID the ticket will contains information about this part of the buying slot
number where the car needs to be parked then the pion spot type and the issue time which is the
time when the vehicle enter the parking lot if we discuss in our requirement that there are two
types of terminals that we have in our parking lot system which are entry terminal and exit terminal
and so we will have a terminal class which would have a get ID as a the class function and then
from this terminal class we would actually drive two different class f1 for the into terminal one
for the exit terminal and you can see right now the entry terminal have caretaker at and
with a parking spot type and then we have an exit terminal X and which accept the ticket and it is
a ticket it takes a ticket as input and these normal I'm saying classes right now these are
either checked losses or interfaces which will be implemented at Iran by the actual class object now
the next set of interfaces and classes that we will discuss will be read to the assignment of
parking spot to a vehicle so we will have an interface called parking assignment strategy
it will have a gate parking spot function which will take a terminal as an input this term that
is the entry terminal so this gate parking spot will return a parking spot and then it will also
have a release parking spot which will take a parking spot as input and of course it will mark
that parking spot as free or available again now and now here we will be using the strategy design
pattern where we actually inherit a parking spot near interest strategy class which will implement
the Gotham to actually figure out the nearest parking spot and they turn that parking spot to
the entering customer and it will be implementing this interface so let us discuss the requirement
again that we had about assignment of parking spot so we discuss in our requirements that we would
like to assign the nearest parking spot to an entering customer from the interest at which then
the customers in into the parking lot so pause the video here and think about it what would be
the best way to implement that parking assignment strategy so a big part of your interview will be
dealing with the assignment of the parking spot and then based on the requirements to
your interview have set in during the interview the design would change for example how you will
assign a parking spot when you have only single intense it's totally different from how you will
assign a parking spot when you have to of all entrances also how will you assign a parking
spot when there's no such requirement about the nearest parking spot how you will implement
the strategy to assign a parking spot when you don't have any requirement to return the nearest
parking spot then to the strategy when you have a requirement to actually return the nice parking
spots totally different in our case we have four parking entrances and we would like to return the
parking spot which is nearest to the interns forum they are the customers entering the parking lot in
my opinion the best way to implement it is using min heaps so we will have a number of min heaps
as the number of entrances and all the parking spots will be added to all those in heaps so
for each entry terminal there will be a min heap this so we will have 4 min heaps for each into
one for each interest and these mean heaps will be actually touring the parking spots in an order of
the distance from the interns apart from that we will have two sets of parking spots one set is for
available parking spots and the other set would be for reserved parking spots so at the beginning
all the parking spots will be available so will exist inside this set now when let's suppose we
have these four instances a b c and d so we will have actually a map of Minh heaps where the key is
the intense ID and the the value is a min heap I mean heap is a data structure which returns
a value which is the smallest of all the values in that data structure so a min heap is a data
structure which is usually implemented in terms of a binary tree where each node has the value which
is less than the value of all of its children and so whenever you actually want to achieve an object
from the min heap it loves it and you the object which has the minimum value and I mean heap for
one parking interest will have the parking spots in it inserted in the order of the distance from
that parking interest so now when the user calls this get parking spot it will give us the parking
terminal from where it is including based on the terminal ID we will retrieve the min heap for that
terminal and then we will just pop the the element from the top of the min heap which will give us a
passing spot which is at the minimum distance from that interest we will mark that parking
spot as reserved we will also remove it from this set of available parking spot and we will move
it into this set of the reserved parking spot we will also remove that parking spot from the mean
heaps of other terminals as well this algorithm will have a computational complexity of K log n
where K is the number of min heaps which is equal to the number of entrances and n is the number of
parking spots in the in this parking lot now I have a question for you let's suppose if the
interviewer change those requirements so now apart from these instances you also have let's
say four elevators in the parking lot and now the interviewer asks you to actually return a parking
spot which is nearest to the entrance from where the customers enter into the parking lot and one
of the elevator as well so you find a parking spot which is nearest to both the entering terminal as
well as the near nearby elevator that mean you in the comment below how will you implement this
requirement we will also have a payment processor interface which has a process function and the
mark and it will also be using strategy design pattern and this interface will be implemented
by different classes which implement different types of payment processing so for example we
could have a credit card payment processor which bosses credit cards we could have a cash payment
processor and in the same way in future let's say if you do some other type of payment for
example Apple P we could actually add another class which will implement the same interface
this is also using the strategy design pattern apart from that we would also have a tariff
calculator and this type of data interface will have calculated tariffs and it will take input as
the time maybe the parking spot type and Litton the dollar amount that should be charged to the
customer and then we could extend this interface by different classes which could extend which
could have different tariffs calculation type for example we could have different tariffs for
weekdays as compared to different tariffs for weekends etc we have discussed in our
comment that we also have monitoring system that monitoring system can actually use the observer
design pattern to actually introduce travel into different components within the system
so if we couldn't we could assume that let's suppose our model system has a has a logger
interface which has a function like log message so you need to understand that since we are using
bottom-up design approach so first we will be discussing the design of individual objects
within the parking-lot system and now we will construct the parking-lot system on the whole
the whole system based on those small components now we will discuss the design of the parking-lot
system itself so the first of all the packing law system itself will have a parking lot class and it
will be using the singleton design pattern because there will be only one instance of the the parking
lot system itself this parking lot system which is single to the object will be composed of different
smaller objects and we have already discussed those objects include entry exit terminals
parking spots the assignment strategy etc the pavement processors and also the printers etc
and we will be using the factory design pattern to actually instantiate all those objects now the
implementation of the parking lot system class itself can take a configuration object as input
and this confusion object may have a different values depending on depending on the requirements
of the parking lot for example and the number of buying spots the type of parking spots in it the
printer settings etc the payment processor settings etc etc and then we can use extra
factory and factory design patterns to instantiate those objects and link them together so in in that
case in our all a parking lot system we will have a set of terminals that we will actually
instantiate the set of terminus that is the set of entry and exit terminals that will instantiate
we will also instantiate a parking assignment strategy and will pass it a configuration object
and from the consideration object that strategy instance will actually retrieve all the parking
spots along with the the parking terminals and the distances of the parking spots from each
entering terminal etc we will also instantiate the different payment processors and we'll pass those
payment processors to the exit terminals because this is the place where we would be processing
the payment we will also instantiate printers based on the the indicate from the configuration
based on the printers in each activity terminal and we will instantiate those objects and we'll
pass those objects to those terminal instance as well so this is how the overall system would be
designed so I'm going to stop there with you here now I'm not going to discuss and actually light
all those class definitions in this video because it's beyond the scope of this video for example
in the strategy class we didn't discuss about how the congruence will be handled which which we can
easily handle through like some synchronization mechanism that locks etc the main thing that I
would wanted to discuss with you guys here was if this design question come what are the things that
you need to look into before designing this and what other things you need to consider the main
purpose of this video was to actually give you some guidelines that if this question comes how
will you solve this question in an actual interview so please do let me know in the
comments below if you find this video useful and also please do like the video and also subscribe
to this channel if you would like to get notified about my future videos thank you and take care