In your HackerRank coding Tests, you can debug your program by using the debug print statements or custom input values to test the output. This article describes how to debug your solution in coding questions that may require you to write a complete program.
Note: If the question requires you to complete the logic for a given function, refer to Debugging your logic in Functions.
Using Debug Statements to Test Your Output
You can test different areas of your program by including the debug print statements and print the output to STDOUT. For example, include print statements such as printf() or Console.WriteLine() to test the output returned at different logical areas of your code.
Example: Consider a coding question to find the sum of two integers. Assuming that your solution does not return the expected output, you may include debug print statements at different logical blocks of your program and then click Run code.
The debug output values are also printed along with the actual output from your program, as shown below.
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 No test cases passed status. When you finish debugging, you must remove or comment on the debug statements and then run your program to validate the question's test cases.
Note: The input and output values are displayed only for Sample Test Cases.
Using Custom Input Values to Test your Program's Output
Click the Custom Input option to debug and test your program using your input values. You can use this option along with debugging statements included in your code.
Note: The Test custom input option is available only if your Test permits it.
Specify custom input values for the program and click Run code. A custom test case executes your program using the given input values. The output from your program and any debug outputs are captured and printed in 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 in the question. In this scenario, the Expected Output area displays the correct output from the hidden solution for the given custom input values.
Example: The hidden solution executes and displays the expected output as shown below:
Note: When you finish debugging, ensure you uncheck the Test against custom input option and remove all the debug statements from your program. Before submitting the answer, 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:
Article is closed for comments.