In technical interviews, especially for software engineering and data science roles, you may be asked to design a system using Object-Oriented Design (OOD) principles. One common scenario is designing a parking lot system. This article will guide you through the process of creating a parking lot system, focusing on key OOD concepts.
Before diving into the design, it’s crucial to understand the requirements. Here are some typical requirements for a parking lot system:
Based on the requirements, we can identify the following key classes:
class ParkingLot:
def __init__(self, capacity):
self.capacity = capacity
self.parking_spots = [] # List of ParkingSpot objects
self.occupied_spots = 0
def park_vehicle(self, vehicle):
# Logic to park the vehicle
pass
def remove_vehicle(self, vehicle):
# Logic to remove the vehicle
pass
class ParkingSpot:
def __init__(self, spot_id, spot_type):
self.spot_id = spot_id
self.spot_type = spot_type
self.is_occupied = False
self.vehicle = None
def park(self, vehicle):
# Logic to park the vehicle in this spot
pass
def remove(self):
# Logic to remove the vehicle from this spot
pass
class Vehicle:
def __init__(self, license_plate, vehicle_type):
self.license_plate = license_plate
self.vehicle_type = vehicle_type
class ParkingTicket:
def __init__(self, vehicle, entry_time):
self.vehicle = vehicle
self.entry_time = entry_time
self.exit_time = None
def calculate_fee(self):
# Logic to calculate the parking fee
pass
class ParkingFeeCalculator:
def calculate(self, entry_time, exit_time):
# Logic to calculate fees based on time
pass
ParkingSpot class should manage its own state (occupied or free).ParkingFeeCalculator should only handle fee calculations.Designing a parking lot system is a common exercise in OOD interviews. By following a structured approach—gathering requirements, identifying key classes, and applying OOD principles—you can effectively demonstrate your design skills. Practice this design and be prepared to discuss your choices and trade-offs during your interview.