I've got into this class called "Competitive Programming," which is basically a preparation for coding contests – preparation by solving and discussing lots of coding problems. Although my humble motivation is to prepare for coding interviews, that I will have to be doing soon, this class is still useful. This ability to solve coding problems is quite alike to a muscle – you have to exercise it regularly to keep it in shape and perform well.So far, it feels more fun than crunching LeetCode alone. I went to a couple of 5-hour contests, and been solving a problem or two every day, realizing that I'm quite out of shape with making loops and matching indices, this thing which is usually called "algorithms". We also have a lecture once a week: our professor is super nerdy and quite hilarious – I miss this type in New York where professors tend to be well-versed in everything, fast spoken, and with a dry sense of humor.
This is how our Prof described the rating of the problems:
- [0, 1200) - try them and see what they give you
- [1200, 1900) – problems to nail for passing the class
- [1900, 2400) - problems for getting a job in big tech
- [2400, 5000) - problems for getting a job as a quant
So quants are on the top of the pyramid.
Another thing is programming languages to use. C++, Java and Python are present, and other languages are allowed too, but our Prof stressed we better choose C++ – it's the best language to understand fundamental algorithms, and doing competitive programming is the best way to learn C++. Alright, I thought, I'll give it another try – it supposedly changed a lot with the new standards. They got for (int element: vector)
iteration syntax now without ugly index++-ing loop – cool... well, not really: once you need the index and not just element in loop, you have to roll back to the ol' good for (int i=0; i<vector.size(); i++)
. Fine. And here is how you pop from a (max)heap:
std::cout << "initial max heap : " << v.front() << '\n';
std::pop_heap (v.begin(),v.end()); v.pop_back();
why can't pop_back()
return the popped element, I don't know. So, it is still quite ugly. But modern C++ being not really modern is part of the problem – the other part is the code I have seen from other students that don't try to write in modern way. But I'll keep doing it for now. Just want to update myself on modern C++. Maybe later I will rewrite the ugliest algorithms in Scala and indulge myself in the coding aesthetics.
The problems seem to hold a tradition of long dorky descriptions: even the easiest problem have quite a story to read. Here is an example of an easy one:
NIT Destroys the Universe:
For a collection of integers, S define mex(S) as the smallest non-negative integer that does not appear in S.
NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.
The universe can be represented as a 1-indexed array 𝑎 of length 𝑛. When NIT snaps his fingers, he does the following operation on the array:
- He selects positive integers l and r such that 1 ≤ l ≤ r ≤ n. Let w = mex({a_𝑙, a_𝑙+1, .., a_r }). Then, for all l ≤ i ≤ r, set a_i to w.
We say the universe is destroyed if and only if for all 1 ≤ i ≤ n, a_i = 0 holds.
Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 0.
No idea who is Thanos, and who is NIT – so, that's quite a world to explore. Definitely more amusing that crunching LeetCode alone.