Debug HackeRank Tests
Facing an interview coding round on HackerRank and desperately need debug tools ? Wait, but HackerRank don’t provides them!
Use this boilerplate to simulate HackerRank’s environment to your local. Do stuff, debug, correct your code and paste the working code back to hackerRank – it’s that simple.
To Make It Work
Once you have cloned the project, here’s the series of steps you need to follow:
- Do “npm i”
- Copy the template code from HackerRank’s website to “testScript.js” (replace old content)
- Write your code, where you need to.
- Write test file under folder “cases” and save them as “.txt”.
- Setup breakpoints
- In VsCode, go to debug pane and run “Launch Program”.
- Play with your code!
- Output, if your code works fine should be found in “results” folder with same name as your test case.
How it Works ?
Internally, hackerRank seems to be providing test-inputs as stdin to the test-script. We are simulating just that.
- We are running through all the test-files provided in “cases” folder one by one, passing whatever is written in it as stream to “stdin” of the child-process, that is spawned out of the test-script.
- Once test-inputs are supplied to stdin, hackerRank’s initial template takes over from there.
- It adds all text chunks, parse them as needed and passes them as args to the function in which you are supposed to write.
- Once you return back the results, it’s written to the results folder which we have passed as environment variables.