Personal evaluation: Simple and basic system design questions, which can be considered to optimize its scalability

🌟 (refer to LeeCode easy 🌟, medium 🌟 🌟, difficult 🌟 🌟)

In addition, algorithm questions usually examine and analyze time complexity and space complexity, which can be referred to the author’s previous article

“Data Structures & Algorithms” series: 3. Time & Space Complexity (2)

Design parking system

Topic describes

Design the parking system

Please design a parking system for a parking lot. There are three different sizes of parking Spaces: large, small and medium, with a fixed number of Spaces for each size.

Implement the ParkingSystem class:

  • ParkingSystem(int big, int medium, int small) initializes the ParkingSystem class with three parameters corresponding to the number of each parking space.
  • Bool addCar(int carType) Checks whether there is a parking space corresponding to carType. Cartypes come in three types: large, medium, and small, denoted by the numbers 1, 2, and 3, respectively. A car can only park in the parking space corresponding to the carType. Return false if there is no empty parking space, otherwise park the car in the parking space and return true.

Example 1:

Input:

[“ParkingSystem”, “addCar”, “addCar”, “addCar”, “addCar”] [[1, 1, 0], [1], [2], [3], [1]]

Output:

[null, true, true, false, false]

Explanation:

ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); Parkingsystem.addcar (2); parkingSystem.addcar (2); Parkingsystem.addcar (3); parkingSystem.addcar (3); // Return false because there are no empty car bits parkingSystem.addcar (1); // Return false because there is no empty parking space and the only parking space is already occupiedCopy the code

Tip:

0 <= big, medium, small <= 1000 If carType is 1, 2, or 3, addCar will be called at most 1000 times

Their thinking

Relatively simple, the basic definition of the type, determine whether there are different types of empty parking space can be

Golang implementation code is as follows:

type ParkingSystem struct {
	big    int
	medium int
	small  int
}

func Constructor(big int, medium int, small int) ParkingSystem {
	return ParkingSystem{big, medium, small}
}

func (this *ParkingSystem) AddCar(carType int) bool {
	switch carType {
	case 1:
		if this.big > 0 {
			this.big--
			return true
		}
	case 2:
		if this.medium > 0 {
			this.medium--
			return true
		}
	case 3:
		if this.small > 0 {
			this.small--
			return true
		}
	default:
		return false
	}
	return false
}

Copy the code

Complexity? The time and space complexity of 🙌 operation is constant O(1).

To optimize the point? Not necessary in terms of complexity, but think about extensibility and possible future usage scenarios as follows:

  • The car pulled out of the parking space
  • If the car is allowed to park in a larger parking space, how to judge and operate?
  • Fixed parking Spaces (private car Spaces) only allow vehicles marked as fixed parking Spaces to enter. How to extend the system design

conclusion

The question is simple, but the following system can be used scenarios can broaden the product thinking, as an interview question is also good ~