• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Multilevel Car Parking System in Python | Coding Design Interview Questions

Aniruddha Chaudhari/1431/0
CodePython

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!

Python Interview Questions eBook

coding challengecoding design
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Why?

Why Competitive Programming Important?

Coding Challenges for Practice

  1. Count Common Factor
  2. Does it Divide
  3. Sum of Sub Arrays
  4. Pair of Desired Sum
  5. Remove Duplicate Char from String
  6. Sort String by Char Freq (Python)
  7. Sort String by Char Freq (Java)
  8. Split Array into Equal Sum Subarray
  9. Validate IP Address
  10. Validate PAN Card Number
  11. Validate Sudoku
  12. Sort Circular Rotated Array
  13. String Permutations
  14. Min Arrow to Burst Bubbles
  15. Min Cost to Paint All Houses [Amazon]
  16. HourGlass with Max Sum
  17. Max Profit by Buying/Selling Stocks
  18. Hailstone Sequence
  19. Reverse String without affecting Special Characters
  20. Secure Conversation by Encry/Decry
  21. Special Elements in Matrix
  22. Next Greater No with Same set of Digits
  23. Smallest Subarray with Sum Greater than Given Number
  24. Group Anagrams
  25. Find Duplicates in Array in O(n)
  26. Find Two Unique Numbers from Array in O(n)
  27. Number Patterns & Finding Smallest Number
  28. First Unique Element in a Stream
  29. Flip Equivalent Binary Trees [TeachMint]
  30. Minimum Cost of Merging Files [Amazon]
  31. Minimum Distance for Truck to Deliver Order [Amazon]
  32. Longest Sequence of URLs
  33. Order Task for Given Dependencies
  34. Design Music Player
  35. Multilevel Parking System Design
  36. Minimum Coins Required
  37. Max Sum Subarray
  38. Max Avg Sum of Two Subsequences
  39. Merge Overlapping Intervals
  40. Longest Balanced Substring
  41. Longest Path in a Weighted Tree
  42. Generate Balanced Parentheses
  43. PostOrder Traversal Without Recursion

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard