Overview
On the HackerRank coding environment, a "Terminated due to timeout" (Time-limit exceeded) message implies that your code is unable to execute and return an output within the preset time limit and hence, aborts to return the error.
For coding questions in your HackerRank Tests, your test setter defines the test cases to compare your code against the expected output. Every time you submit or run your code, input from your program is used in the test cases to return an expected output. If your code is not quick and efficient to read input and process output, it gets aborted with the "Terminated due to timeout" error. It could also mean that your code is running into infinite computations, index overflow, or input overload, causing the timeout.
Note: The input from your code is read into all the defined test cases to check for output, and the "Terminated due to timeout" message is returned when any of the test cases could not be executed to return output.
Time Limits on the HackerRank Coding Environment
The HackerRank coding environment supports about 35 different programming languages, and for each language, there is a definite memory limit and a specific time limit for code execution. Your test setter expects optimal solutions in your code and hence exists the time limit for code execution on our environment.
To overcome and avoid the error, refer to the HackerRank Coding Environment Specifications Page, to know the specific memory and time limit for your chosen programming language. Accordingly, optimize your code for faster execution to achieve output. You can also access the specifications page from your HackerRank tests.
Note: The code execution time limit is varied for different programming languages. Ensure that you refer to the HackerRank environment specifications page for the time limit specified for your chosen language. For instance, Python has a longer time limit of 10 seconds rather than the C language, which requires code execution within 2 seconds.
Example: In the C language code, you could be using loops to perform a recursive check on a set of values, and each of these values can be the input to the test cases. In the illustration shown below, the sample C language program should read 10 integers and return the sum of the integers.
The following program to read 10 integers and add them can return a "Terminated due to timeout" status because of the input overhead in code.
The test cases could not return output because the logic involved an iterative check, resulting in an input overload.
When you change the logic in your code to check the index value more efficiently, your input is faster to all the test cases, and the output is returned within the time limit. See the illustration below:
In HackerRank Tests, it is recommended that you adhere to efficient and optimal coding practices to achieve faster output for your coding solutions.
Best practices for optimal coding:
Practices |
Examples |
Avoid compulsive code iterations. |
Avoid using multiple nested loops in your code when you have a large data set to compare. |
Ensure faster approaches to read input to test cases and write the output. |
In Java, when working with multiple threads, use the BufferReader class instead of Scanner. In Python, use the stdin.readline() and stdout.write() instead of input and print. |
Ensure that the input value to test cases is passed in the expected format. |
If the expected input is a date value, ensure that the date value is passed in the expected format, such as dd/mm/yy or mm/dd/yyyy. |
Ensure that your code addresses the logic to execute the edge scenarios in the test cases and not just the straightforward cases. |
Your C code to read and add 10 numbers may fail when test cases include floating value input to produce the output. You must optimize your code to read integer or floating input values and produce the desired output. |
Use efficient approaches such as algorithms and data structures. Avoid the brute-force method for computations. |
When you are searching for elements in an array, avoid sequential search and use the binary search method. |
Include timestamps in your code. |
You can use the language-specific time-stamp functions to identify the areas in your code which are taking longer to execute and optimize them accordingly. |