- Install Go
sudo apt-get update
sudo apt-get -y upgrade
wget https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz
sudo tar -xvf go1.16.4.linux-amd64.tar.gz
sudo mv go /usr/local
- Setup Go Env
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
- Clone Project
git clone https://github.com/sudaraka94/valet-parking.git
cd valet-parking
- Run Tests
go test ./...
- Run the program
go build -o .
./main
- Run the program with a custom data file
./main -data=<path to datafile>
- Clone the repository
git clone https://github.com/sudaraka94/valet-parking.git
cd valet-parking
- OPTIONAL : Replace the
datafile
inside project directory with the desired datafile - Build docker image
docker build --tag valet-parking .
- Run on docker
docker run -it valet-parking
- You can find all the utility methods inside
util.go
file - I tried to minimize the usage of third party libraries and do most of the implementations myself
Here is the basic algorithm used in this program. We can separate out the algorithm into four sections,
- Look for an available slot with the least slot number -
Time Complexity: O(n)
- Mark the slot as occupied -
Time Complexity: O(1)
- Keep reference of the slot in a hashmap with the vehicle registration number -
Time Complexity: O(1)
- Lookup the vehicle in the hashmap using the vehicle registration number -
Time Complexity: O(1)
- Remove the vehicle from the hashmap and mark the slot available -
Time Complexity: O(1)
- Calculate the fare -
Time Complexity: O(1)
Throughout the whole project, Logger is exposed through an interface. Currently logger interface is implemented by cliLogger, which directly writes to the std out. But in case we need to writeout the results to a file, we can easily do so by introducing a Logger implementation which writes to a file. Logger type can be made configurable via config.yml
logger_config:
logger_type: "cli" #allowed logger types: cli
In this application, supported types of vehicles are configurable. On the application start,
it fetches configurations from confi.yml
and adjusts accordingly. For an example, if we need to
add a third vehicle type we just have to define the vehicle type in the config.yml
.
eg:
vehicle_types:
- name: car
price_per_hour: 2 #pricePerHour in USD
- name: motorcycle
price_per_hour: 1 #pricePerHour in USD
- name: van
price_per_hour: 3 #pricePerHour in USD