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 “Test against 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.
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. 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.
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 format of input to your code and returning the output in the exact expected format. If Sample Test cases are available for your coding question, download the Sample Test Cases and view the sample input and output files to understand the expected input value and the output from your code.
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.
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 given 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.
Comments
1 comment
How does our code know to look at the "test against custom input" area rather than the file that's uploaded? I copied about half the rows from the CSV file that has been uploaded for me into the area for testing against custom input. I don't know how to adapt my code - which currently looks at the complete file that's uploaded - to look at the "custom input" area instead.
Can anyone suggest an idea?
Please sign in to leave a comment.