How Online Judges and Contests Work
Learn the rules of the game — how to read problems, submit solutions, and get that sweet AC!
Open interactive version (quiz + challenge)Real-world analogy
What is it?
An online judge is an automated system that tests your code against predefined test cases and gives you instant feedback. You submit your source code, the judge compiles it, runs it with secret inputs, compares your output to the expected output, and returns a verdict. The whole process usually takes just a few seconds. It is like having a robot teacher that grades your work instantly!
Real-world relevance
Automated testing (just like an online judge) is used everywhere in the software industry. When engineers at Google or Netflix write code, automated test systems check their code against thousands of test cases before it goes live. This is called Continuous Integration/Continuous Deployment (CI/CD). The same concept powers CP judges — it is real-world software engineering!
Key points
- Reading a Problem Statement — Every CP problem has a problem statement — a description of what you need to solve. It tells you the story (sometimes a fun one about wizards or robots), what input you will receive, what output you must produce, and any rules or limits. Read it slowly and carefully — at least twice! Many wrong answers happen because people misread the problem.
- Understanding Input Format — The input section tells you exactly what data your program will receive. For example, 'The first line contains an integer N' means your program should read one number first. 'The next N lines each contain two integers' means you then read N pairs of numbers. Following the input format exactly is crucial — the judge sends input in precisely this format.
- Understanding Output Format — The output section tells you exactly what your program should print. If it says 'Print a single integer,' you must print just one number — nothing else! No extra words, no extra spaces, no 'The answer is:' in front. The judge compares your output character by character with the expected answer. Even one extra space can cause WA (Wrong Answer)!
- Sample Test Cases — Your Best Friends — Every problem comes with sample test cases — example inputs and their correct outputs. These are gifts! Before submitting, always run your code with the sample inputs and check if your output matches exactly. If your code does not pass the samples, it definitely will not pass the hidden test cases. Samples are your free preview of the test.
- Hidden Test Cases — Besides the samples, the judge has many hidden test cases that you cannot see. These test tricky situations: very large numbers, very small numbers, edge cases (like zero or one), and special patterns. Your code must handle ALL of them correctly to get AC. This is why testing only with samples is not enough — you must think about corner cases too.
- Time Limit and Memory Limit — Every problem has a time limit (usually 1-2 seconds) and a memory limit (usually 256 MB). Your program must finish within the time limit and use less memory than the memory limit. If your code is too slow, you get TLE (Time Limit Exceeded). If it uses too much memory, you get MLE (Memory Limit Exceeded). This is why writing efficient code matters!
- How Contests Work — In a contest, you get several problems (usually 4-8) and a fixed time (usually 2-3 hours). Problems are roughly ordered from easiest to hardest. You earn points for each problem you solve. Most contests use penalty time — you get extra penalty minutes for wrong submissions and for the time it took to solve. So being fast AND correct matters!
- Common Beginner Mistakes to Avoid — Here are mistakes that catch everyone at first: printing extra text like 'Enter a number:' (the judge does not want that), forgetting to read all the input, printing the wrong number of spaces or newlines, and not handling edge cases. The golden rule: print EXACTLY what the problem asks for and nothing more.
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
// A typical CP problem:
// Read two numbers, print their sum.
// Input: two integers on one line
// Output: one integer (their sum)
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}Line-by-line walkthrough
- 1. #include — our trusty toolbox line that gives us all of C++. We use this in every CP solution.
- 2. using namespace std; — our shortcut so we can write cout and cin without the std:: prefix.
- 3. int main() { — the starting point of our program. The judge will run everything inside this function.
- 4. int a, b; — we create two memory boxes named 'a' and 'b', both capable of holding whole numbers (integers). We are declaring two variables at once by separating them with a comma.
- 5. cin >> a >> b; — we read two numbers from the input. The judge will send us the input, and this line captures it. The >> operator reads one value at a time, so this reads a first, then b second.
- 6. cout << a + b << endl; — we calculate a + b and print the result, followed by a new line. Notice we print ONLY the answer — no extra text, no 'The sum is:', just the number. This is crucial for getting AC!
- 7. return 0; — we tell the system our program finished successfully. Always include this at the end of main().
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << "The sum is: " << a + b << endl;
return 0;
}