### ToxicPie9's blog

By ToxicPie9, 2 months ago,

Yesterday YouKn0wWho posted a blog explaining common mistakes in competitive programming and how to avoid them. I was greatly inspired by that post so I decided to make my own.

I have compiled some of the mistakes that I didn't make in my early Competitive Programming phase. I also mentioned how to avoid them. Also, in most cases, I will give you a chance to find out what the bug is before I reveal the culprit as I tried to make this blog interactive. The codes that I have used in this blog have been written in Rust as it is the most beloved language for CP.

#### Mistake 1

Check out the following code:

Code

The output should be $10^{18}$. But if you run the code, you will get a different output. Why?

Reason

#### Mistake 2

Check out the following code:

Code

Try to run this locally. What is the time complexity of this?

Is it $O(n)$?

Reason

#### Mistake 3

Check out the following code:

Code

Notice that it is guaranteed that total sum of n is <= 100000. So how many operations will the code take in the worst case?

Reason

#### Mistake 4

What is happening in the following code?

Code

The output is supposed to be [1, 1, 1, 1, 1]. But it's not the case actually! Why?

Reason

#### Mistake 5

Don't use endl! If your code needs to print millions of newlines, then using endl turns out to be really slower than using '\n'. Why?

Reason

#### Mistake 6

Use pow() function for integer calculations.

Why?

#### Mistake 7

Run the following code

Code

You might expect the output to be $-1$. But the output is actually not! Why?

Reason

#### Mistake 8

Using eprintln! might be a good way to debug your code as it doesn't output to the standard output. But leaving the eprintln! instances in your code while submitting in OJ might be one of the worst ways of getting TLE.

#### Mistake 9

Look at the following code

Code

The output is $0$, which is correct. Why?

Reason

#### Mistake 11

Consider the following code for calculating the maximum occurrence in an array.

Code

This code seems like it can be hacked easily but in fact, for almost every valid input, it should get AC.

Why?

#### Mistake 12

Run the following code:

Code

What will be the size of the map? $5$?

Check

#### Mistake 13

I forgot Mistake 10.

#### Mistake 14

Check this out.

Code

What is the time complexity of this?

Check

#### More Mistakes and Non-Mistakes

• Do not insert or erase from a container (Vec, HashSet etc) while traversing it using for x in s.iter() like syntax at the same time. This is because the compiler won't let you do that. In Rust, you are not allowed to borrow a variable while it is also borrowed as mutable at the same time (e.g. when inserting).
• Create variables only when you need them instead of just declaring let a, b, c, d, e, f, g, h; and 69 other variables at the beginning of your code! This is because Rust's grammar doesn't let you do that.
• If you want to count the number of set bits in an i64 number, then use the count_ones() method instead of the __builtin_popcount function. This is because there is no __builtin_popcount function.
• If you want to compute the square root of an f64 number, then use the sqrt() method instead of sqrt() because sqrt() function takes self as input whereas sqrt() takes self.
• Speaking of Runtime Error, the most likely case of getting Runtime Error is when your code encounters an error while running.
• let x: i64 = 1 << 40; will not overflow as Rust will try to deduce the types of integer literals if they're not specified. In this case, the compiler determines that 1 is i64.
• Don't accidently write your code in C++.

Thanks for reading the blog. Feel free to add more mistakes that are common in CP in the comment section.

See you on a later episode, friend blobheart!

P.S. Although this is mostly a joke post, I tried to be accurate about the facts and did not intentionally put incorrect information in it (which means you may treat it as a somewhat educational blog). It also aims to showcase some features of Rust that helps you avoid some common CP mistakes in languages like C++.

• +337

By ToxicPie9, history, 5 months ago,

Hello Codeforces!

I have created a Discord server for the upcoming ICPC World Finals in Dhaka, so the finalists can have a place to chat with each other.

You can join it with the link: https://discord.gg/p3W9YCZ3AK

• +54

By ToxicPie9, history, 18 months ago,

sus has just overtaken Errichto and became the second top contributor on Codeforces. During the last few days, his contribution points skyrocketed at a terrifying rate.

Will sus eventually top Monogon and become the new ruler of Codeforces? Or is the power of Monogon's comments simply unmatched by mere mortals?

• +14