Parking Lot
medium · classes + state, allocation
Brief-reported interview attribution (unverified): Amazon, Microsoft, Uber, Google
How to approach an LLD roundDesign a parking lot with several floors. Each floor has spots of three
sizes: compact, regular, large. Vehicles are bike, car, truck. A bike
fits any spot, a car fits regular or large, a truck fits large only. Parking
takes the first fitting free spot in floor order, then spot order. A ticket records
vehicle type, floor, spot, and entry time. Fees are per hour: bike $1, car $2,
truck $4. Timestamps are integers (hours) passed by the caller.
class ParkingLot:
def __init__(self, floors):
# floors: list of dicts, e.g. [{"compact": 2, "regular": 2, "large": 1}]
def park(self, vehicle_type, timestamp):
# -> ticket id (int, starting at 1), or None if no fitting spot / bad type
def unpark(self, ticket_id, timestamp):
# -> fee (int) = (timestamp - entry) * rate, or None if the ticket is unknown/used
def free_spots(self, floor, vehicle_type):
# -> number of free spots on that floor that fit the vehicle (0 for a bad floor/type)
Rules. A vehicle that does not fit anywhere returns None from park. unpark
with an unknown or already-used ticket returns None and changes nothing. A car
must not take a compact spot while a regular one is free.
Money units: fees are whole dollars, not cents; a three-hour car stay returns 6.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Public demo
# Public demo: run after implementing the class API above.
p = ParkingLot([{'compact': 1, 'regular': 1, 'large': 1}, {'compact': 0, 'regular': 1, 'large': 0}])
print(p.park('bike', 0))Starter code
class ParkingLot:
def __init__(self, floors):
"""floors: list of dicts, e.g. [{"compact": 2, "regular": 2, "large": 1}]"""
pass
def park(self, vehicle_type, timestamp):
"""-> ticket id (int, starting at 1), or None if no fitting spot / bad type"""
pass
def unpark(self, ticket_id, timestamp):
"""-> fee (int) = (timestamp - entry) * rate, or None if the ticket is unknown/used"""
pass
def free_spots(self, floor, vehicle_type):
"""-> number of free spots on that floor that fit the vehicle (0 for a bad floor/type)"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler