algos-backend

API for https://github.com/kunalpanchal/algos-frontend based on Node.JS

Algo (Frequent Words Count)

Run locally and test

cd algos-backend && npm i && npm start

For testing run :

npm run test

alt screenshot-1

Routes

GET /get-frequent-words?input=99
GET /get-frequent-words?input=99&useRedis=true

Pseudo Code

1.Check if request inputs are valid

2.If useRedis is not specified and perform a get request to the URL and get the raw data.

3.Perform data sanitization

* replace all the newline,tabs and return carriages
* replace special characters with space
* replace numbers with space
* replace multiple spaces with a single space
* trim the data 

4.Split the data with a space and save it to a arr.

5.Create a hashMap from array with frequency saved as its value.

6.Sort the hashmap.

7.Return the required number of top frequencies

Complexity

n = total number of words in the file test.txt

m = total number of unique words in the file test.txt

COMPLEXITY => O( n + m + mlog(m) ) => O( n + mlog(m) )

The use of Redis

Whenever the values are computed using the above algorithm, they are saved to Redis.And it can be used to retrieve the data faster.Speed comparisson:

Without Redis With Redis

Dependencies Used

"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"dotenv": "^4.0.0",
"express": "~4.15.2",
"express-promise-router": "^2.0.0",
"redis": "^2.8.0"

// Dev Dependencies
"chai": "^4.1.2",
"chai-http": "^3.0.0",
"mocha": "^4.0.1"