A microservice that sorts a list of flights then outputs the source and final destination path among the list.
On the terminal cd to the project folder then run make, an executable named app should be generated at the project folder.
Example (build and run):
$ cd project_folder
$ make
$ ./app
Install the go toolchain, please follow the instructions in this link.
With go properly installed, open a terminal and run the following commands to build the app:
cd path_to_project_folder
go build -o app
The flights microservice expects json-encoded POST requests on the /calculate endpoint.
The body must be in the following format:
{
"flights": [["SRC1", "DST1], ["SRC2", "DST2"], ...]
}
Example request in curl:
$ curl -X POST http://localhost:8080/calculate \
-H 'Content-Type: application/json' \
-d '{"flights":[["ATL", "EWR"], ["SFO", "ATL"]]}'
["SFO","EWR"]
On a terminal, run the app command built during the building stage.
By default, the http server listens on port 8080.
If another port is required, just set the API_PORT enviroment variable to the desired port number.
Example running on a custom port on Linux:
$ API_PORT=3000 ./app
On a terminal, run the make test command.
The presented solution assumes that the input consists of a list of source/destination pairs, whose data set compose a directed graph.
Here is the solution outline:
- Create a tracker object, a map that tracks each airport by its key, e.g. "SFO", "ATL", etc.
- Each entry in the tracker map has a corresponding integer value, whose initial value is zero.
- Iterate over each source/destination airport pair in the input list.
- For the source element in the pair, decrement the counter value by 1 in the tracker.
- For the destination element in the pair, increment the counter value by 1 in the tracker.
For a non-ciclical graph, after processing each element, the tracker map must have one element whose counter is -1, this is the starting airport.
Each element with a value of zero are airports in the middle.
The ending airport will have a value of 1.
By processing the tracker map and looking for the min,max counters we have the starting/endpoint airports.