This article describes how to debug your solution in Coding Questions which require you to write a complete program, and explains how debugging output values are captured in the HackerRank coding environment.
If your solution involves completing a function, refer to the Debugging your logic in Functions topic.
Test different areas in your program by including debug print statements in your code to print the output to STDOUT. For instance, you can insert print statements such as printf() or Console.WriteLine() in different areas of your code and test your logic are different steps.
In the HackerRank coding environment, when you write and debug a program, the debug output values are not captured separately but printed in the Your Output area itself. Therefore, when you run your program, the debug output values are also compared with the expected output of the test cases, which may result in a "Wrong Answer" status. When you finish debugging, you must remove or comment the debug statements and then run your program to validate the Question's test cases.
Note: Before you submit your final program in the test, ensure that you comment or remove all the debug statements from your program and verify that your program returns only the actual output which is compared with the expected output of the test cases.
Example: Consider a coding Question which requires you to write a program to read any number and return the sum of the digits as the output. Assume that your program is not returning the expected output and you want to debug your program.
To test your code, include debug print statements at different logical blocks in your code and Run your program. The output values returned by the different debug statements are also captured and printed in the Your Output area.
You can also debug and test your program using the “Test against custom input” option provided for coding questions. Use this option to input different values to your program and test your output. Along with custom inputs, you can also include the debug print statements in your code to print your debug output.
Note: The Test against custom input option is available for debugging only if your test setter has permitted this option in your Test.
When you run your program with custom inputs, the Question's test cases are not executed. Only the output value from your program and any debug outputs are captured and printed in the Your Output area.
In some cases, to help you to understand the expected output for the problem, your test setter may have included a hidden solution to the problem. When you use custom input values, these values are also executed against the hidden solution to display the Expected Output value for your custom input. You can refer to the Expected Output value to understand the required solution to the problem and improve your program accordingly.
Note: The Expected Output value is displayed for your custom input only if a hidden solution is provided for the problem.
Example, consider the same program to read any number and return the sum of the digits as the output. Test the program using custom input values and verify if the output returned is correct and as per the expected solution.
As shown above, the program output and the debug outputs are captured in the Your output area. The output from the hidden solution is shown in the Expected Output area.
When you finish debugging, ensure that you uncheck the "Test against custom input" option, remove all the debug statements and print your output in the exact expected format. Before submitting your program, it is recommended that you re-run your program and validate the test cases for successful execution.
For a detailed understanding of coding question test cases, refer to the following topics: