• Explore
  • About Us
  • Log In
  • Get Started
  • Explore
  • About Us
  • Log In
  • Get Started

Parking Lot Manager Simulation

In this exercise, you will simulate a parking lot system where vehicles arrive and depart asynchronously. The parking lot has a fixed capacity, and your program will manage parking and departure processes concurrently using goroutines and channels.

Requirements

  1. The parking lot should be implemented as a buffered channel, with the capacity representing the number of available parking spaces (e.g., 3 spaces).
  2. Vehicles should arrive at the parking lot one by one, attempting to park:
    • If a parking space is available, the vehicle parks, and a message is logged.
    • If the parking lot is full, the vehicle waits, and a message is logged indicating it is waiting for a space.
  3. Vehicles should leave the parking lot at regular intervals:
    • A message should be logged each time a vehicle departs, freeing up a parking space.
  4. The simulation should run for a fixed duration (e.g., 15 seconds) and terminate gracefully.
  5. Use len() and cap() functions to dynamically manage and log the parking lot's current occupancy and total capacity.
  6. The implementation must avoid panics or runtime errors when the program exits.

Example Behavior:

When the program runs, the output might look like this:

[INFO] Parking lot initialized with 3 spaces.
[ARRIVAL] Vehicle 1 is attempting to park...
[PARKED] Vehicle 1 has parked. (Spaces left: 2)
[ARRIVAL] Vehicle 2 is attempting to park...
[PARKED] Vehicle 2 has parked. (Spaces left: 1)
[ARRIVAL] Vehicle 3 is attempting to park...
[PARKED] Vehicle 3 has parked. (Spaces left: 0)
[ARRIVAL] Vehicle 4 is attempting to park...
[WAITING] Parking lot full. Vehicle 4 is waiting for a space.
[DEPARTURE] Vehicle 1 has left. (Spaces left: 1)
[PARKED] Vehicle 4 has parked. (Spaces left: 0)
...
[INFO] Simulation ended.

Program Structure

Project structure should look like this:

.
├── main.go

Your program should have the following structure and implement these functions:

main.go

package main

import (
	"fmt"
	"time"
)

// newParkingLot creates a new parking lot with the specified capacity
func newParkingLot(capacity int) chan int {
	fmt.Printf("[INFO] Parking lot initialized with %d spaces.\n", capacity)
	return make(chan int, capacity)
}

// parkVehicles simulates vehicles arriving and parking in the parking lot
func parkVehicles(parkingLot chan int) {
	
}

// leaveVehicle simulates vehicles leaving the parking lot
func leaveVehicle(parkingLot chan int) {
	
}

func main() {
	parkingLot := newParkingLot(3) // Create a parking lot with 3 spaces

	// Start goroutines for parking and departure
	go parkVehicles(parkingLot)
	go leaveVehicle(parkingLot)

	// Run the simulation for 15 seconds
	time.Sleep(15 * time.Second)
	fmt.Println("[INFO] Simulation ended.")
}

Acceptance Criteria

  1. Vehicles should park if there is space available and print a message indicating the action.
  2. Vehicles should wait if the parking lot is full, and print a message indicating they are waiting.
  3. Vehicles should leave the parking lot at regular intervals, freeing up space.
  4. The program should run for 15 seconds and exit gracefully.
  5. No panics or errors should occur during execution.

Hints

  • Use cap() and len() functions to check the capacity and current occupancy of the channel.
  • Use time.Sleep() to simulate random delays for arrivals and departures.
  • Goroutines will terminate automatically when main() exits, so there’s no need for explicit shutdown logic.

    Golang (Go) for Production Systems

    Unlock All Exercises

  • Getting Started
    • Important - Please Read
  • Error Handling
    • Finance Transaction Error
    • Resilient Retry Mechanism
    • Graceful Panic Recovery
    • Error Wrapping and Unwrapping
    • Aggregated Validation Errors
    • Transaction Rollback on Failure
  • Interfaces
    • Design a Payment Gateway Interface
    • Pluggable Logging System
    • Configurable Notification System
    • Dynamic Data Serializer
    • Dynamic Plugin System
  • Concurrency
    • File Word Counter
    • Traffic Lights Controller
    • Parking Lot Manager
    • Real-time Auction System
    • Safe Bank Account Balance Update
    • Build a Buffered Logger
    • Build a TTL Cache
    • Build an Elevator Control System
    • Debug and Fix Race Conditions - Part 1
    • Debug and Fix Race Conditions - Part 2
    • Debug and Fix Race Conditions - Part 3
    • Dynamic Feature Flag System
    • Real-Time Multiplayer Matchmaking
    • Lazy Database Connection
    • Distributed Task Deduplication
    • High Performance Request Counter
  • Core Networking
    • Simple TCP Echo Server
    • TCP Number Guessing Game
    • Looking Up Domain Information
    • UDP Time Broadcast Service
    • UDP Ping-Pong Client and Server
    • Building a Simple TCP Chat Server and Client
  • Winding Up
    • Final Notes