Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Design Parking System
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Design a parking system for a parking lot that has three types of parking spaces: big, medium, and small. Each of these parking spaces has a limited number of slots available.

Implement Solution class constructor and the below methods:

  1. Solution(int big, int medium, int small): Initializes an object of the Solution class. This constructor receives the total number of available slots for each type of parking space: big, medium, and small.

  2. bool addCar(int carType): This method takes in an integer carType, which can be either 1 (big), 2 (medium), or 3 (small). It checks whether there is a free parking space available for the specified car type. If a slot is available, the car is parked, and the method returns true; otherwise, it returns false.

Examples

Example 1:

  • Input:
    ["Solution", "addCar", "addCar", "addCar", "addCar", "addCar"]
    [[2, 1, 1], [1], [2], [3], [2], [1]]

  • Expected Output: [null, true, true, true, false, true]

  • Explanation:

    • The parking system is initialized with 2 big slots, 1 medium slot, and 1 small slot.
    • addCar(1): A big car parks successfully (true).
    • addCar(2): A medium car parks successfully (true).
    • addCar(3): A small car parks successfully (true).
    • addCar(2): There is no parking slot available for medium car. So, it returns false.
    • addCar(1): A big car parks successfully (true).

Example 2:

  • Input:
    ["Solution", "addCar", "addCar", "addCar", "addCar"]
    [[0, 2, 1], [3], [2], [2], [1]]

  • Expected Output: [null, true, true, true, false]

  • Explanation:

    • The parking system is initialized with 0 big slots, 2 medium slots, and 1 small slot.
    • addCar(3): A small car parks successfully (true).
    • addCar(2): A medium car parks successfully (true).
    • addCar(2): Another medium car parks successfully in the remaining medium slot (true).
    • addCar(1): A big car cannot park because there are no big slots available (false).

Example 3:

  • Input:
    ["Solution", "addCar", "addCar", "addCar", "addCar"]
    [[1, 0, 1], [2], [3], [3], [1]]

  • Expected Output: [null, false, true, false, true]

  • Explanation:

    • The parking system is initialized with 1 big slot, 0 medium slots, and 2 small slots.
    • addCar(2): A medium car cannot park because there are no medium slots available (false).
    • addCar(3): A small car parks successfully (true).
    • addCar(3): All small car slots are occupied. So, it can't be parked.
    • addCar(1): A big car parks successfully in the only available big slot (true).

Constraints:

  • 0 <= big, medium, small <= 1000
  • carType is 1, 2, or 3
  • At most 1000 calls will be made to addCar

Solution

To solve this problem, we maintain an array availableSlots that stores the number of available parking slots for big, medium, and small cars. The array is initialized with the number of slots for each type provided during the instantiation of the Solution class. When a car tries to park, we check if there is an available slot of the corresponding type by accessing the appropriate index in the availableSlots array. If a slot is available, we decrement the count of that slot and return True, indicating that the car has parked successfully. If no slots are available, we return False.

Step-by-Step Algorithm

  1. Initialization:

    • Create a class named Solution.
    • Inside the class, define an integer array availableSlots to keep track of the available parking slots for each car type (big, medium, small).
    • Implement the constructor Solution(int big, int medium, int small):
      • Initialize the availableSlots array with three elements: big, medium, and small, representing the number of slots available for big, medium, and small cars, respectively.
  2. Adding a Car:

    • Implement the addCar(int carType) method:
      • This method will check if there is an available slot for the car type specified by carType.
      • If the corresponding slot value in the availableSlots array is greater than 0, decrement the value by 1 (indicating one slot is now occupied) and return true to indicate successful parking.
      • If the slot value is 0 (indicating no slots are available for that car type), return false.

Algorithm Walkthrough

Let's walk through the given input step-by-step:

Input:
["Solution", "addCar", "addCar", "addCar", "addCar"]
[[2, 1, 1], [1], [2], [3], [2], [1]]

Steps:

  1. Step 1 - Initialize Parking System:

    • The Solution class is initialized with 2 big slots, 1 medium slot, and 1 small slot.
    • availableSlots array is set to [2, 1, 1].
  2. Step 2 - Add First Car (Big Car):

    • The first addCar(1) is called to park a big car.
    • The method checks availableSlots[0] (big car slots), which is 2.
    • Since there is space available, the method decrements availableSlots[0] by 1. Now, availableSlots is [1, 1, 1].
    • The method returns true, indicating successful parking.
  3. Step 3 - Add Second Car (Medium Car):

    • The second addCar(2) is called to park a medium car.
    • The method checks availableSlots[1] (medium car slots), which is 1.
    • Since there is space available, the method decrements availableSlots[1] by 1. Now, availableSlots is [1, 0, 1].
    • The method returns true, indicating successful parking.
  4. Step 4 - Add Third Car (Small Car):

    • The third addCar(3) is called to park a small car.
    • The method checks availableSlots[2] (small car slots), which is 1.
    • Since there is space available, the method decrements availableSlots[2] by 1. Now, availableSlots is [1, 0, 0].
    • The method returns true, indicating successful parking.
  5. Step 5 - Add Fourth Car (Medium Car):

    • The fourth addCar(2) is called to park another medium car.
    • The method checks availableSlots[1] (medium car slots), which is 0.
    • Since no slots are available for medium cars, the method returns false, indicating the car cannot be parked.
  6. Step 6 - Add Fifth Car (Big Car):

    • The fifth addCar(1) is called to park another big car.
    • The method checks availableSlots[0] (big car slots), which is 1.
    • Since there is space available, the method decrements availableSlots[0] by 1. Now, availableSlots is [0, 0, 0].
    • The method returns true, indicating successful parking.

Final Output:

The final output for this sequence of operations is [null, true, true, true, false, true].

Code

Python3
Python3

. . . .

Complexity Analysis

Time Complexity:

  • The time complexity for both the ParkingSystem constructor and the addCar method is O(1).
    • Initialization: The constructor only initializes an array with three elements, which takes constant time.
    • addCar: Each call to addCar involves a simple check and possible decrement of an array element, both of which take constant time.

Space Complexity:

  • The space complexity of the ParkingSystem class is O(1).
    • The space required is constant since we only maintain a fixed-size array with three elements to store the number of available slots for big, medium, and small cars.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible