Bootcamp problems tips&tricks

Table of Contents

Here follows a list of tips&tricks for students who are attending for the first time the programming test on codeforces.

The first thing you should do is read and understand the problem statement. Usually most of it is just a useless long story, so try to focus on the important part of the text. For example, in the following problem (easy problem of 1st test 2024 edition): alt text You can notice without having to carefully read the whole text that what you need to do is just calculat s//d. The first part of the problem statement is just a random story put there to make the problem more interesting, nothing more.

How to write the solution

The code that we write should take the input from stdin (without printing anything extra, like we are used to do in our university courses, like input('Enter a number: ')), and print the result as it is expected in the output. For the problem to be considered accepted the output should match EXACTLY the expected output, character by character.

In this problem A - Ratatouille for the example input our solution must print

5
160

If it prints something different, like

5.0
160.0

or

Solution:
5
160

Then it’s considered wrong.

How to use the templates

Usually we provide templates for the most common languages (2023/2024 edition: python, c, c++, java). The templates contain a main that automatically parses the input, and a function solve that you have to implement.

For example, for the problem A - Ratatouille the template for python is:

def solve(d, s) -> None:
    # MODIFICA SOLO QUESTA FUNZIONE
    # print(res)
    return

if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        d, s = map(int, input().split())
        solve(d, s)

Depending on the problem and on the template, you must understand if it’s needed to return the result or to print it. Here the function solve must print the result, and we can understand this by the facts that:

  • The function solve has a return type None
  • There is the line commented # print(res)
  • If we run the code with return we don’t get anything
  • In the main there is no print(solve(d, s)), only solve(d, s)

A correct solution for this problem using the template would be:

def solve(d, s) -> None:
    # MODIFICA SOLO QUESTA FUNZIONE
    print(s//d)
    return
...

Running locally our code

How can we be sure that the solution is actually printing the correct thing? Just run it locally!

Once we’ve written a solve or a partial solve, to test it, we must setup a way to run it locally. If we don’t do that then the only option that we have is submitting 20times on codeforces and trying to understand what it means when it displays Wrong answer on test 1.

Codeforces uses stdin and stdout for input and output, the equivalent of input() and print() in python, so you should be able to run the solution from your editor or terminal. For example, once we have a solution that calls input and print we can just run it and interact by copy-pasting the example input to our terminal: alt text In this case, we know that the solution at least will be partially correct, as the known test case will be correct.

For linux users (or wsl users) we can avoid copy-pasting by using the following command:

python3 solution.py < input.txt

This will run the solution reading the input from the file input.txt (instead of stdin) and printing the output to the terminal. alt text

Extensions

Another trick that we can use (and that i personally follow) is to install some extensions in the editor that tests the solution against the example test cases, and automatically checks for correctness. in vscode there is a cool extension called Competitive Programming Helper (cph) that simplify our life. I strongly suggest to install it, as it will save you a lot of time and effort. alt text

To use this in vscode, we first start by creating a file where we will write the solution, for example A.py. Then, in the lateral bar, we click on the cph icon and clikc on the big button Create Problem: alt text Now we can just insert the example input and output in the respective fields and click on Run or Run All: alt text Getting the result: alt text

Online you can find some resources that provide detailed instruction on how to install and use at best the cph extension, also connecting directly to the codeforces account for automatic submissions and automatic parsing of the problem statements (not required, i don’t personally use this feature).

Test cases

In codeforces there is the mechanism of hidden test cases, which are the hidden inputs (test 3, test 4, …, test n) that the solution will be tested against. Those unknown test case usually check for edge cases or big inputs, so it’s important to have a solution that is efficient and correct not only for the first one.

It is very common to write a solve that works for the example input, but fails after for some unknown reason.

If after submitting a solution we get a Wrong answer on test X, there is no way to know what was the input for which our solve failed. We can only guess and try to understand what went wrong. This either by creating some local test case to put in the CPH extension/input file, or by trying to debug the program in the classical way.

Codeforces Results

In codeforces there are multiple output that we can get after submitting a solution:

  • Accepted: This is what we want to see. Our solution is correct and efficient, and we are done with that problem.
  • Wrong answer on test Y: The solution passes some test cases (if Y is like 2,3,4,…) but fails on some other. We need to understand what went wrong and fix it.
  • Time limit exceeded on test Y: The solution is correct but too slow. Probably the algorithm developed is not optimal. For example you might have written a nested for loop (O(n^2)) instead of a more elaborate solution that can run with a sorting (O(nlogn)) or a single pass (O(n)).
  • Memory limit exceeded on test Y: The solution is correct but uses too much memory, probably you are allocating a huge array or list that is not needed.
  • Runtime error on test Y: The solution is raises an exception, like a division by zero or an index out of range.
  • Compilation error: The solution doesn’t compile, meaning that you have a syntax error (as forgetting some parenthesis or colon).