Multilevel Car Parking System in Python | Coding Design Interview Questions

Multilevel Car Parking System in Python | Coding Design Interview Questions

In this tutorial, we are going to design multilevel car parking system. I will also explain the different coding design interview questions asked in the interview.

Requirement:

  • There is N number of parking levels in multilevel car parking.
  • On each level, there would be x number of parking slots.
  • Assume the number of slots is the same on each level.

This question was asked in the coding design round of the Treebo Hotels interview.

You can choose any programming language of your choice to get the solution to this problem.

Python Program

I’m solving it in Python. If you understand the logic, you can write a code and solve it in any programming language.

We are going to implement a multilevel car parking system in Python using object-oriented concepts.

class ParkingSlot():
    def __init__(self):
        self.N = 5
        self.X = 4
        self.total_slots = self.N*self.X
        self.park_data = [("", "") for i in range(self.total_slots)]
        self.booked = 0

    #parking space allotment
    def park(self, reg_no, color):
        if self.booked < self.total_slots:
            tup = ()
            ind = self.park_data.index(("", ""))
            self.park_data[ind] = (reg_no, color)
            print(f"Vehicle is parked at {ind}.")
            self.booked += 1
        else:
            print("Parking full.")

    def unpark(self, reg_no):
        out = [slot for slot in self.park_data if slot[0]==reg_no]
        if out:
            self.booked -= 1
        self.park_data.remove(out[0])

    def fetch_slot_by_reg(self, reg_no):
        out = [slot for slot in self.park_data if slot[0]==reg_no]
        print(f"Parking slot for {reg_no}:{out}")

    def fetch_slot_by_col(self, col):
        out = [slot for slot in self.park_data if slot[1]==col]
        print(f"Parking slot of all the cars of color {col}:{out}")

        def prnt(self):
        print(f"\n{self.park_data}")


if __name__ == "__main__":
    obj_park = ParkingSlot()
    obj_park.park("Reg1", "White")
    obj_park.prnt()
    obj_park.park("Reg2", "Red")
    obj_park.prnt()
    obj_park.park("Reg3", "Blue")
    obj_park.prnt()
    obj_park.unpark("Reg2")
    obj_park.prnt()
    obj_park.fetch_slot_by_reg("Reg1")
    obj_park.prnt()
    obj_park.fetch_slot_by_col("Blue")

Here, I’m using the list compression technique to manage the parking slots (aka Python list). Get familiar with some of the Python tricks for competitive programming. These tricks will be very handy to solve this kind of question very effectively.

Output:

Vehicle is parked at 0. 
[('Reg1', 'White'), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')] 

Vehicle is parked at 1. 
[('Reg1', 'White'), ('Reg2', 'Red'), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')] 

Vehicle is parked at 2. 
[('Reg1', 'White'), ('Reg2', 'Red'), ('Reg3', 'Blue'), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')] [('Reg1', 'White'), ('Reg3', 'Blue'), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')] 

Parking slot for Reg1:[('Reg1', 'White')] 
[('Reg1', 'White'), ('Reg3', 'Blue'), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')] 

Parking slot of all the cars of color Blue:[('Reg3', 'Blue')]

The code is self-explanatory. If you still have any doubt, you can ask me in the comment section.

As this is the coding design round, the aim is not just to write a code to solve the problem but it should be scalable to tackle the different scenarios for the real parking system.

Also, check the design music player using Python. This question was asked in the Amagin coding design round interview.

Coding Design Questions on Multilevel Car Parking System

Here are some of the coding design questions asked by the interviewer for the multilevel car parking system.

If you want to crack coding design interviews, it is not just about writing code. It is also about how you answer these questions.

1. How o store more details about the vehicle like the number of wheels, color, etc.?

In the above example, we are only storing the color of the vehicle. To make this system more scalable and to allow storing other vehicle features, we can create a class-like structure for vehicles.

Example:

class vehicle():
  def init(self, reg_no, col, wheels):
    self.reg_no = reg_no
    self.col = col
    self.wheels = wheels

Save the class object in the parking slot list.

2. How to manage each parked booking details like entry/exit time, the number of hours slot booked for, etc.?

Just like the vehicle class, we can also create a class-like structure for slot detail.

Example:

class booking():
  def init(self, book_time, booking_hrs=24):
    self.book_time = book_time #timestamp
    self.booking_hrs = booking_hrs
    self.out_time = book_time + booking_hrs

Save the class object in the parking slot list.

3. How to get the list of parking slots where the color of the car is “blue”?

This is one of the interesting questions. You can simply loop over all the parking slots and check the color of the car. But, this is not the optimal solution.

We can use the hash table with the key as “color” and value as “slot number”. While booking the parking slot, make the entry in the hash table. Now we can get parking slots having specific car colors just by looking into the hash table. (Note: Hash table can be implemented in Python using Python dictionary.)

4. How to find the parking level from the slot?

Parking slots in the Python list are stored in sequential order. Suppose there 5 levels and 20 slots on each level. In the Python list, the first 20 slots are there for the first level, the next 20 slots are there for the second level, and so on.

Here the index in the Python list is nothing but the parking slot number. To get the level of parking for a particular slot, just divide it by the number of slots per level. Suppose slot number is 50, the parking level is 50/20 = 2. (Parking level starts from zero.)

This is all about Multilevel Car Parking System coding design questions and answers. The interviewer can also ask you to optimize any scenario so be prepared for that. If you have any doubt, let me know in the comment. All the best!

Leave a Reply

Your email address will not be published. Required fields are marked *