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:
The output should be $$$10^{18}$$$. But if you run the code, you will get a different output. Why?
Mistake 2
Check out the following code:
Try to run this locally. What is the time complexity of this?
Is it $$$O(n)$$$?
Mistake 3
Check out the following 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?
Mistake 4
What is happening in the following code?
The output is supposed to be [1, 1, 1, 1, 1]
. But it's not the case actually! Why?
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?
Mistake 6
Use pow()
function for integer calculations.
Mistake 7
Run the following code
You might expect the output to be $$$-1$$$. But the output is actually not! Why?
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.
Smash me for more info.
Mistake 9
Look at the following code
The output is $$$0$$$, which is correct. Why?
Mistake 11
Consider the following code for calculating the maximum occurrence in an array.
This code seems like it can be hacked easily but in fact, for almost every valid input, it should get AC.
Mistake 12
Run the following code:
What will be the size of the map? $$$5$$$?
Mistake 13
I forgot Mistake 10.
Mistake 14
Check this out.
What is the time complexity of this?
More Mistakes and Non-Mistakes
- Do not
insert
orerase
from a container (Vec
,HashSet
etc) while traversing it usingfor 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 thecount_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 thesqrt()
method instead ofsqrt()
becausesqrt()
function takesself
as input whereassqrt()
takesself
. - Speaking of
Runtime Error
, the most likely case of gettingRuntime 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 that1
isi64
.- 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++.