Purposefully vague. Discuss the exact requirement with your interviewer.
Some questions we can ask:
How should accessibility be taken care of? Do we need to prioritize handcap parking areas, for example?
Is this a ground parking lot, or a monitored system?
How many entrances are in the parking lot? (Do we have to deal with any issues of concurancy?)
Should there by any pricing strategies in place? Do we offer premium services (reserved parking areas) to customers?
Do we need to support different types of spots for different type of vehicles?
Assumptions:
Design a monitored system for support for 4 different type of vehicles (S,M,L,XL). The parking lot supports 150 small vehicles, 100 medium vehicles, 50 large vehicles, and 25 extra large vehicles.
A smaller vehicle can only be parked in a spot that is designated to be equal or large than itself. That is a L vehicle cannot park to M spot, but can park to an L to XL spot.
Implementation:
Build an interface and support efficient implementation for the following methods:
Spot placeVehicle(Vehicle):return a spot for a Vehicle spot.
This method must return an available spot prioritized by its type. For example, a small vehicle can only be parked in a mediumed sized parking spot iff there are no small vehicle spots available.
If there are no spots available, throw an exception.
void removeVehicle(Vehicle): remove a vehicle from the parking lot.
from enum import IntEnumimport queue, copyclassSize(IntEnum): S =0, M =1, L =2, XL =3classVehicle:def__init__(self,id,size):""" :param id: [int] :param size: [Size] """ self.drivers_lic =id self.size = sizedef__hash__(self):returnhash(self.drivers_lic)def__eq__(self,other):return self.drivers_lic == other.drivers_licdef__str__(self):returnstr(self.drivers_lic)+" "+str(self.size)classCar(Vehicle):def__init__(self,id):""" :param id: [int] """super().__init__(id, Size.M)def__hash__(self):returnsuper().__hash__()def__eq__(self,other):returnsuper().__eq__(other)def__str__(self):returnsuper().__str__()classMotorCycle(Vehicle):def__init__(self,id):""" :param id: [int] """super().__init__(id, Size.S)def__hash__(self):returnsuper().__hash__()def__eq__(self,other):returnsuper().__eq__(other)def__str__(self):returnsuper().__str__()classBus(Vehicle):def__init__(self,id):""" :param id: [int] """super().__init__(id, Size.XL)def__hash__(self):returnsuper().__hash__()def__eq__(self,other):returnsuper().__eq__(other)def__str__(self):returnsuper().__str__()classTruck(Vehicle):def__init__(self,id):""" :param id: [int] """super().__init__(id, Size.L)def__hash__(self):returnsuper().__hash__()def__eq__(self,other):returnsuper().__eq__(other)def__str__(self):returnsuper().__str__()classSpot:def__init__(self,id,size):""" :param id: [int] :param size: [Size] """ self.id =id self.size = sizedef__eq__(self,other):return self.id == other.idclassParkingLot:def__init__(self,zipcode,size_class):""" :param zipcode: [int] :param size_class: List[int] """ self.zipcode = zipcode self.capacities = size_class self.counts = [0] *4# main datastructure to hold are parking cars self.Park_DB ={Size.S:{}, Size.M:{}, Size.L:{}, Size.XL:{}}# using a priority queue will ensure that when a car gets# removed from the lot, the next available spot will be the# next 'closest' spot to the next user. self.spot_ids = queue.PriorityQueue()foridinrange(sum(self.capacities)): self.spot_ids.put(id)defplace_vehicle(self,vehicle):""" Identifies and stores the next available parking spot :param vehicle: [Vehicle] :return: [Spot] """# check if vehicle already exists in the lotifany(vehicle in self.Park_DB[size] for size in Size):raiseValueError("Duplicate Vehicle ID identified.")# find the first available spot starting from current sizefor size inrange(int(vehicle.size), len(self.counts)):if self.counts[size]< self.capacities[size]:# a spot was found, so add the spot to the hash table next_spot =Spot(self.spot_ids.get(), Size(size)) self.Park_DB[Size(size)][vehicle] = next_spot self.counts[size]+=1return next_spotraiseOverflowError("All vehicle capacities are full.")defremove_vehicle(self,vehicle):""" Removes an identified vehicle from the parking lot :param vehicle: [vehicle] :return: [Spot] """for size inrange(int(vehicle.size), len(self.counts)):if vehicle in self.Park_DB[Size(size)]:# the vehicle was found - copy and return its spot spot = copy.copy(self.Park_DB[Size(size)][vehicle])# put back the available parking id spot back into queue self.spot_ids.put(spot.id)# remove the vehicle from the lotdel self.Park_DB[Size(size)][vehicle] self.counts[size]-=1return spot# vehicle was not foundraiseLookupError("Vehicle id was unidentified in the ParkingLot")def__str__(self): strings = []for size, vehicles in self.Park_DB.items(): strings.append(str(size) +": "+"\n")for vehicle in vehicles: strings.append("\t"+str(vehicle) +"\n")return''.join(strings)