Last active
November 2, 2023 03:09
-
-
Save h2rashee/ac6f67a96c1cdaa94a7e25f07acc6618 to your computer and use it in GitHub Desktop.
AtoB: Design a Parking lot that can handle multiple vehicle types based on certain rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # Design a parking lot | |
# # Assumptions: | |
# # - The parking lot can hold motorcycles, cars and vans. | |
# # - The parking lot has spots of 3 sizes: small spots, medium spots and large | |
# # spots. | |
# # - A motorcycle can park in any spot. A car can park in a single medium spot, | |
# # or a large spot. | |
# # - A van can park, but it will take up 3 medium spots or a large spot. For the | |
# # purposes of this exercise, don’t worry about if the medium spots are next to | |
# # each other. | |
# # - These are just a few assumptions. Feel free to ask your interviewer about | |
# # more assumptions as needed. | |
# # Please implement a parking lot with following functionality: | |
# # - The ability to park a vehicle in the parking lot. | |
# # - The ability to remove (unpark) a vehicle that has been previously parked. | |
class ParkingLot: | |
VEHICLE_PARKING_RULES = { | |
"MOTORCYCLE": [("SMALL", 1), ("MEDIUM", 1), ("LARGE", 1)], | |
"CAR": [("MEDIUM", 1), ("LARGE", 1)], | |
"VAN": [("LARGE", 1), ("MEDIUM", 3)] | |
} | |
def __init__(self, small_spots, medium_spots, large_spots): | |
self.TOTAL_SPOT_CAPACITIES = {"SMALL": small_spots, "MEDIUM": medium_spots, "LARGE" : large_spots} | |
self.current_occupancy = {} | |
def park(self, vehicle_type, vin): | |
if vin in self.current_occupancy: | |
return False | |
for rule in self.VEHICLE_PARKING_RULES[vehicle_type]: | |
# spot_type, spot_taken_amount = rule | |
if self.TOTAL_SPOT_CAPACITIES[rule[0]] >= rule[1]: | |
self.TOTAL_SPOT_CAPACITIES[rule[0]] = self.TOTAL_SPOT_CAPACITIES[rule[0]] - rule[1] | |
self.current_occupancy[vin] = (rule[0], rule[1]) | |
return True | |
return False | |
def unpark(self, vin): | |
if vin not in self.current_occupancy: | |
return False | |
vehicle_type = self.current_occupancy[vin][0] | |
vehicle_spots_taken = self.current_occupancy[vin][1] | |
self.TOTAL_SPOT_CAPACITIES[vehicle_type] = self.TOTAL_SPOT_CAPACITIES[vehicle_type] + vehicle_spots_taken | |
del self.current_occupancy[vin] | |
return True | |
def pretty_print(self): | |
return self.TOTAL_SPOT_CAPACITIES | |
# 1s, 4m, 1l | |
# park moto -> 1s | |
# park car -> 1m | |
# park van1 -> 1l | |
# park van2 -> 3m | |
# unpark van1 -> 1l cleared up | |
# pl = ParkingLot(1, 4, 1) | |
# print(pl.pretty_print()) | |
# print(pl.park("MOTORCYCLE", "M1")) | |
# print(pl.pretty_print()) | |
# print(pl.park("CAR", "C1")) | |
# print(pl.pretty_print()) | |
# print(pl.park("VAN", "VAN1")) | |
# print(pl.pretty_print()) | |
# print(pl.park("VAN", "VAN2")) | |
# print(pl.pretty_print()) | |
# print(pl.unpark("VAN1")) | |
# print(pl.pretty_print()) | |
# print(pl.park("VAN", "VAN3")) | |
# print(pl.pretty_print()) | |
# print(pl.park("MOTORCYCLE", "M2")) | |
# print(pl.pretty_print()) | |
pl2 = ParkingLot(0, 1, 4) | |
print(pl2.pretty_print()) | |
print(pl2.park("MOTORCYCLE", "M1")) | |
print(pl2.pretty_print()) | |
print(pl2.park("MOTORCYCLE", "M2")) | |
print(pl2.pretty_print()) | |
print(pl2.park("CAR", "C1")) | |
print(pl2.pretty_print()) | |
print(pl2.unpark("C2")) | |
print(pl2.pretty_print()) | |
print(pl2.park("CAR", "C1")) | |
print(pl2.pretty_print()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment