Parking Lot Simulator - Problem

Design a parking lot management system that can handle multiple floors and parking spots. The system should support basic operations like parking a vehicle, removing a vehicle, and finding where a vehicle is parked.

Your system should handle the following operations:

  • park(vehicle_id): Park a vehicle and return the assigned spot location
  • unpark(vehicle_id): Remove a vehicle from its parking spot
  • find_vehicle(vehicle_id): Find the location of a parked vehicle

The parking lot has a fixed number of floors and spots per floor. Vehicles should be assigned to the first available spot starting from floor 0, spot 0.

Return the results of all operations as a list of strings, where each string represents the result of the corresponding operation.

Input & Output

Example 1 — Basic Parking Operations
$ Input: floors = 2, spots_per_floor = 2, operations = ["park A1", "park B2", "find_vehicle A1", "unpark A1", "park C3"]
Output: ["Floor 0, Spot 0", "Floor 0, Spot 1", "Floor 0, Spot 0", "Vehicle A1 removed", "Floor 0, Spot 0"]
💡 Note: A1 parks at first spot (0,0), B2 at (0,1), finding A1 returns (0,0), after unparking A1 that spot becomes available for C3
Example 2 — Full Floor Scenario
$ Input: floors = 2, spots_per_floor = 1, operations = ["park X1", "park Y2", "park Z3"]
Output: ["Floor 0, Spot 0", "Floor 1, Spot 0", "No available spots"]
💡 Note: First two cars fill both floors (each has 1 spot), third car cannot be parked
Example 3 — Vehicle Not Found
$ Input: floors = 1, spots_per_floor = 2, operations = ["find_vehicle X1", "unpark Y2"]
Output: ["Vehicle not found", "Vehicle not found"]
💡 Note: Both operations fail because no vehicles are parked yet

Constraints

  • 1 ≤ floors ≤ 100
  • 1 ≤ spots_per_floor ≤ 100
  • 1 ≤ operations.length ≤ 1000
  • Vehicle IDs are unique strings of length 1-10

Visualization

Tap to expand
INPUTALGORITHMRESULTParking Layout2 floors × 2 spots each(0,0)(0,1)(1,0)(1,1)Operationspark A1park B2find_vehicle A1unpark A1park C31Initialize Data Structures• Parking grid (2D array)• Vehicle location hash map• Available spots queue2Process Each Operation• park: pop from available queue• find: O(1) hash map lookup• unpark: remove from map3Update Structures• Sync all data structures• Maintain consistency• Return operation resultOperation Results"Floor 0, Spot 0""Floor 0, Spot 1""Floor 0, Spot 0""Vehicle A1 removed""Floor 0, Spot 0"↑ Instant responsesPerformance✓ O(1) per operation✓ No searching needed✓ Scales to large lotsKey Insight:Use separate hash maps for vehicle locations and available spots to achieve O(1)operations instead of O(n×m) searching through the entire parking grid.TutorialsPoint - Parking Lot Simulator | Hash Map Approach
Asked in
Google 35 Amazon 42 Microsoft 28 Apple 18
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen