Lesson 3 of 50 beginner

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

An online judge is like a super-fast teacher who grades your homework instantly. You hand in your solution (submit your code), and within seconds, the teacher checks it against the answer key (test cases) and tells you your grade (verdict). Unlike a real teacher, this one never gets tired, never goes on vacation, and grades millions of papers every day!

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

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. 1. #include — our trusty toolbox line that gives us all of C++. We use this in every CP solution.
  2. 2. using namespace std; — our shortcut so we can write cout and cin without the std:: prefix.
  3. 3. int main() { — the starting point of our program. The judge will run everything inside this function.
  4. 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. 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. 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. 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;
}
Need a hint?
The code calculates the correct sum, but think about what the online judge expects in the output. Does the problem say to print extra words?
Show answer
The bug is printing extra text 'The sum is: ' before the answer. Online judges compare your output exactly with the expected output. If the expected output is just '7' but you print 'The sum is: 7', the judge sees different strings and gives you WA (Wrong Answer). Remove the extra text and just print a + b.

Explain like I'm 5

Imagine you are taking a test, and the teacher has an answer key. You write your answers, hand in your paper, and the teacher checks every answer against the key. If all your answers match perfectly, you get a gold star (AC). If any answer is wrong, you get 'try again' (WA). If you take too long writing, the teacher says 'time is up!' (TLE). That is exactly how an online judge works!

Fun fact

Codeforces has over 600,000 registered users from nearly every country in the world, and it has hosted over 2,000 contests since it launched in 2010. The platform runs hundreds of test cases for each submission — sometimes over 100 tests for a single problem! The servers can handle thousands of submissions per minute during contests. That is some serious computing power!

Hands-on challenge

Go to Codeforces and try to solve your first problem! Go to codeforces.com/problemset and filter by difficulty 800 (the easiest). Pick a problem that asks you to add two numbers or do something simple. Read the problem statement carefully, check the sample input/output, write your code, test it with the samples, and submit it. Getting your first AC is one of the best feelings ever!

More resources

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero