In your HackerRank Test Coding Questions, your test setter may have already defined a method or function which you must complete with your solution to return the expected output value(s). For example, a coding Question may require you to complete a function defined as:
int Sum(int num1, int num2)
You must write the logic in this function to find the sum of two integers defined in variables num1 and num2.
This article suggests how to debug your logic in a function using Debug Print Statements and Custom Input Values. You will also understand how the debug output values from the predefined function definitions are captured in the HackerRank coding environment.
If your solution involves writing a complete program, refer to the Debugging a complete program topic.
Test the logic in your function by including debug print statements to print the output to the STDOUT stream. For instance, you can insert print statements such as printf() or Console.WriteLine() in different areas of your code and test your logic at different steps.
In the HackerRank coding environment, when you debug a predefined function, the debug output values are captured separately and printed in the Debug Output area.
Example: Consider a coding Question which requires you to complete the function:
int Sum(int num1, int num2) to calculate the sum of integers.
Assume that your program is not returning the expected output and you have included debugging print statements in your code to test your logic. When you run your code successfully, the test cases are executed and the debug statement output is captured separately shown below:
You can also debug and test your functional logic using the “Test against custom input” option. Use this option to input different values to your function 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 code with custom inputs, the Question's test cases are not executed. Only the output value from your function is printed in the Your Output area and any debug outputs are captured and printed in the Debug Output area.
Example, for the function to find the sum of two integers, as shown above, test the program using custom input values and verify the output returned by your function.
As shown above, the function's logical output and the debug outputs are captured separately.
Before you submit your solution, it is recommended that you uncheck the "Test against custom input" option, remove all the debug statements, re-run your code to execute all the test cases successfully, and verify the output.
For a detailed understanding of coding question test cases, refer to the following topics: