In your HackerRank coding Tests, you can debug your program by using debug print statements or custom input values to test the output. This article describes how to debug your solution in coding questions that require you to write a complete program.
Note: If the question requires you to complete the logic for a given function, refer to the Debugging your logic in Functions topic.
Test different areas in your program by including debug print statements and print the output to STDOUT. For instance, 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 is not returning the expected output, include debug print statements at different logical blocks of your program and 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.
Click the "Custom Input" option to debug and test your program using own 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 permitted in your Test.
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 that you uncheck the "Test against custom input" option and remove all the debug statements from your program. Before submitting the answer, we recommend 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: