This approach uses a Trie and a Priority Queue
- Setup a buffered I/O Reader to scan for words
- Discarded words w/ nonalphabetic characters
- Split words into a list of characters
- Insert characters into Trie tracking frequency until all valid words are consumed
- Start HTTP server and wait for GET
- After successful request validation, get prefix's sub-Trie
- To find up to 25 of the sub-Trie's most frequent words, firstly initialize a Priority Queue whose comparator is frequency
- Preorder traverse the sub-Trie and insert into the Queue when node's frequency is greater than 0(denoting end-of-word)
- Finally return at most 25 from the Priority Queue
- HTTP GET request w/ prefix as a URL parameter
- Ignore non-word entities
- Top 25 results ordered by frequency
- Only use the Golang standard library
- Clone the repo
- Install Golang:
brew update && brew install golang - Run the service with
make run
After cloning the repo and installing Golang, run the test scripts: make test
GET /autocomplete?prefix=<prefix>
Response:
{
"words": []string,
}
This gets the 25 most frequent words starting with the prefix
Once the server is running:
curl -X GET http://localhost:9000/autocomplete\?prefix\=thDefault port of 9000 can be modified in the makefile
sample data: ["th", "fr", "pi", "sh", "wu", "ar", "il", "ne", "se", "pl"]
To follow along, run make examples which will call a shell script and display results to stdout
