Design a Parking Lot System: OOD Interview Walkthrough

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.

Requirements Gathering

Before diving into the design, it’s crucial to understand the requirements. Here are some typical requirements for a parking lot system:

  1. Parking Lot Types: The system should support different types of parking lots (e.g., regular, compact, oversized).
  2. Parking Spots: Each parking lot should have multiple parking spots, which can be occupied or free.
  3. Vehicle Types: The system should accommodate various vehicle types (e.g., cars, motorcycles, trucks).
  4. Parking Fees: The system should calculate parking fees based on the duration of stay.
  5. Entry and Exit: The system should manage vehicle entry and exit, ensuring that the parking lot does not exceed its capacity.
  6. Notifications: The system should notify users when a parking spot becomes available.

Identifying Key Classes

Based on the requirements, we can identify the following key classes:

  1. ParkingLot: Represents the parking lot itself.
  2. ParkingSpot: Represents an individual parking spot within the parking lot.
  3. Vehicle: Represents a vehicle that will park in the parking lot.
  4. ParkingTicket: Represents a ticket issued to a vehicle when it parks.
  5. ParkingFeeCalculator: Responsible for calculating the parking fees based on time.

Class Design

1. ParkingLot Class

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

2. ParkingSpot Class

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

3. Vehicle Class

class Vehicle:
    def __init__(self, license_plate, vehicle_type):
        self.license_plate = license_plate
        self.vehicle_type = vehicle_type

4. ParkingTicket Class

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

5. ParkingFeeCalculator Class

class ParkingFeeCalculator:
    def calculate(self, entry_time, exit_time):
        # Logic to calculate fees based on time
        pass

Design Considerations

  • Encapsulation: Each class should encapsulate its data and behavior. For example, the ParkingSpot class should manage its own state (occupied or free).
  • Single Responsibility Principle: Each class should have a single responsibility. For instance, the ParkingFeeCalculator should only handle fee calculations.
  • Open/Closed Principle: The design should allow for future extensions, such as adding new vehicle types or parking lot types without modifying existing code.

Conclusion

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.