Disclaimer: This article was written few years ago and may no longer be relevant as software engineering has changed a lot in the last few years. This is what may be more relevant now: Future of Software Engineering - Gaurav ChandakMost of competitive programming and DSA interview practice is done on online coding platforms. Some of the most popular interview practice and competitive programming platforms are Codeforces, Codechef, Leetcode, Geeksforgeeks etc. Most of them follow a common pattern and work in similar ways.
You are given a problem and you are expected to submit code which solves the problem.
In this article, you will learn about:
- How code evaluation works on online judges?
- Why should you test your solution before submitting?
- How to understand the problem statement?
- How to understand the error codes on online judges?
- How to effectively solve a DSA problem on online coding platforms?
How code evaluation works on online judges?
Let’s understand how online judges evaluate your code. You will be able to solve problems better and faster if you understand how they work.
Each problem has a set of input and expected output files stored behind the hood. Say I1, I2 ... In are input files and EO1, EO2 … EOn are output files.
When you submit your code, it is run against all these input files. This generates a list of generated output files, say, GO1, GO2 … GOn.
The generated output files are compared with the corresponding expected output files. If they match, your solution is deemed to be correct.
But a lot could go wrong along the way. Code in compiled languages need to be compiled before they are run. Your code could fail in the compilation step.
Your solution has to work under some pre-defined constraints associated with time and memory. If it breaks those constraints, the execution is stopped.
Your code could also encounter issues and break at run-time. In all such cases the judge will return an error message which you can use to fix your code. Learn about error messages in error codes section.
Why should you test your solution before submitting?
In most cases you won’t have to submit your code to know if it has issues. Most judges provide a test button which can be used to, well, test your code. This is usually complimented with an option to provide custom input. When you “test” your code, it is compiled and then run against this custom input. This can be used to ensure that your code has no compilation errors. The judge also returns the output that is generated by your code. By trying out different input values, you can manually check if your code is producing output that you expect it to do.
It is a good practice to test and verify your code before you submit. Many platforms impose penalties for wrong submissions. It reduces your accuracy. In competitions, the penalties have implications on your score and ranking.
Understanding the problem statement
When you open up a problem on a programming (problem solving) website, you are likely to encounter the following components:
Problem Statement
This is a description of the problem. You are supposed to write code that solves this problem.
Input and Output Format
This defines how your code should take input and what the format of the output generated by your code should be. You need to ensure that you follow this format strictly, otherwise your solution won’t be accepted even if its correct logically.
Sample Input and Expected Output
In order to assist you in understanding what the problem expects of you, you will often find examples in the form of sample input and expected output. Use this as an example for input & output format as well.
Constraints
These are the constraints (rules) that apply to the input values. This will help you in designing solutions which are optimal for these size of inputs. These are guaranteed by the the problem setter and you don’t need to check for them explicitly in your code.
Limits
These are the time and memory limit that your code must abide by. These are the resources allocated for your code to run. If your code exceeds these limitations, you will encounter an error.
If your code encounters an issue, your output does not match expected result or you exceed limits defined, you will encounter an error on submission. To understand what those errors are, read the error codes section.
Understanding the error codes
It’s quite common to face errors when you test or submit your code on online judges. It can be challenging to understand what is wrong with your code and fix it. However online judges usually return an error code or a message which can help you understand more about what caused the failure.
Below are some of the most common errors seen on online judges, what they mean, and some of the common reasons why they occur.
Compilation Error (CE):
Your program did not get compiled successfully. Try to understand the issue based on the Compilation Error Message. Common reasons for CE:
- Syntax Error
- Missing Imports
- Using restricted functionalities
Wrong Answer (WA):
Your program ran successfully but returned a different output than what was expected. Common reasons for WA:
- Incorrect interpretation of problem.
- Incorrect solution.
- Bug in the code.
- Edge cases. Program is tested on cases not mentioned in the problem statement. Make sure that you have identified and tested your code with multiple edge cases.
- Incorrect output format (Different from format mentioned in problem)
Time Limit Exceeded (TLE):
Your program did not complete execution in the alloted time. Your program gets a predefined time limit for every test case. If it takes more time, it is killed. TLE does not guarantee that your program is correct. Common reasons for TLE:
- Solution is not optimal.
- Infinite Loop.
Memory Limit Exceeded (MLE):
Your program tried to allocate more memory than it was allowed to. Common reasons for MLE:
- Declaring large arrays or lists.
- Adding a lot of data.
- Stack Overflow Error
Runtime Error (SIGSEGV):
Your program tried to access or write to a memory that it cannot access or is invalid. Also known as Segmentation Fault. Common reasons for SIGSEGV:
- Accessing array/string index outside its range.
- Using too much memory in some languages.
- Uninitialized or incorrectly initialized pointers.
Runtime Error (SIGFPE):
Your program encountered a floating-point error. Generally caused if you do an invalid math operation. Common reasons for SIGSEGV:
- Division by zero.
- Square root/Log of negative numbers.
Runtime Error (SIGABRT):
Your program aborted the program due to fatal error. Common reasons for SIGABRT:
- Using assert or abort in the code.
Runtime Error (NZEC/Non-zero error code):
Your program did not return a zero-error code from the main method and did not fall into any of the above buckets. Common reasons for NZEC:
- Not returning 0 from main method.
- Not catching exceptions.
How to effectively solve a DSA problem on online coding platforms?
These are the steps that you should follow:
- Understand the question completely.
- Get an estimate of the required complexity.
- Come up with edge cases based on the constraints.
- Come up with a brute-force solution. Verify if it will pass.
- Optimize, verify and repeat this step.
- Dry-run your solution on the sample tests and edge cases.
- Code and test with the sample tests and edge cases.
- Submit. Debug and fix, if the solution does not work.
Read a more comprehensive guide here: How to solve a DSA problem on online coding platforms?.
---
I hope that you found this article helpful. Make sure to bookmark it and reference it from time to time. It might seem a bit overwhelming. Most of these should become part of your muscle memory once you start using them regularly.
---
Please reach out to me on WhatsApp at





