In HackerRank Coding Tests, if your solution is not able to generate the expected output, you can debug your logic using custom input values to locate errors and logical issues.
The “Custom Input” option is provided for coding questions using which you can add custom input values to your code and test the output. You can also include the debug print statements in your code to verify the logic.
The question's test cases are not executed when you run your program with custom inputs. A custom test case executes your logic using the given input values. The output from your program and any debug outputs are captured and printed.
For example, consider the function to calculate the sum of all elements in an array. Assuming that your logic in the function is not able to produce the expected output, you can debug the logic using custom input values as shown below:
The output returned by your code with the given custom input values is shown below:
Tip: While debugging, it is also important to check that you are passing the correct input format to your code and returning the output in the expected format. Suppose Sample Test cases are available for your coding question.
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. The Expected Output area displays the correct output from the hidden solution for the given custom input values in this scenario.
In the following example, the problem contains the hidden solution to calculate the sum of two integers. The Expected Output value is displayed from the hidden solution for the custom input value.
Note: The solutions to all HackerRank library questions are uploaded. Therefore, candidates can compare their output with the expected output when they add the input to test their code for these questions.
Refer to the Custom input values format topic to know the different methods to add custom input values to test your code.