How An Online Judge Works And How To Avoid Time Limit Exceeded Problem?
In this article, you will get consent on how an online judge works and this article discusses very frustrating error Time Limit Exceed error that coders get at some point in time while solving the questions at online platforms. The article primarily discusses three things:
- How does an online judge work?
- Why do we get TLE?
- How to avoid TLE?
How Does An Online Judge Work?
There are various online judges Hackerrank, HackerEarth, Codechef. All of the have their respective algorithms and systems of evaluating submissions. Let’s consider an online judge who is accepting the submission for a given problem. For this submission, the judge has some input and some output files loaded in it already. The judge passes the submission to the processor and this processor has some processing limits. Let’s say in 1 sec the processor is able to perform 10^8 operations. Now, what happens internally when you submit your program is that your program executes and it gets the input from the processor and verifies the output with already loaded test cases in it. If it matches then it is a case of a correct answer but sometimes during the execution, if the program takes more time than the required time limit then it prompts the user time limit exceeded error. For example, if the required limit was 1 second and your program were taking more than 1 second for execution, then your judge will issue a kill command and your output will be killed and in the output, you will get Time Limit Exceeded (Optimize Your Code). This is how an online judge works.
Why Do We Get TLE?
When your algorithm doesn’t have the required efficiency you get TLE so the idea is to complete the processing in a finite amount of time. Let’s assume you are calculating whether a number is prime or not and the number is of the order 10^18. If you are using the O(n) algorithm you are bound to get TLE and if you use the O(log N) algorithm you won’t get TLE. Below are some common reasons for TLE error…
- Online judges put some restrictions on time and it doesn’t allow you to process your instruction after that time limit. If you take more time than the time limit which is specified than you will get the TLE error.
- TLE error also depends on the server architecture, operating system, and the complexity of an algorithm. For different platform server architectures are different and the speed of the execution for the code varies on each server.
- If the programmer is using a slow method of reading and writing the input in the code then it will give you a TLE error.
How To Avoid TLE?
1. Analyze the constraints: If the time limit is 1 sec, your processor is able to execute 10^8 operations.
2. Choose faster input and output method. For example:
- Use buffer reader in java, do not use Scanner
- In C++ use scanf/printf instead of cin/cout,
- Use these two statements in python for speeding up your execution
3. Your program must not contain 4 nested loops if N<=100.
4. Sometimes too many nested loops can make your program slower. So it’s better to optimize your code and reduce the number of loops according to the instruction of input bound already specified in the question. So read the bounds in your code carefully and according to that write your program.
5. Optimize your algorithm or try to find a different solution for the problem statement using another data structure.
As the saying goes, the experience is the best teacher. The more you code the algorithms, the more efficient the geek you become in the coding.
Important Link: How to overcome Time Limit Exceed(TLE)?