
Competitive Programmer's Handbook (2017) [pdf] - linouk23
https://cses.fi/book.pdf
======
zerr
The most irritating thing with these competitive/algo stuff is that no matter
how many times you master it - eventually you always forget it, because you
don't need it on a daily (or more like yearly) basis in the real world.

~~~
gaius
That’s the entire point of those kinds of interviews. It’s a strong filter for
“recent graduate” while maintaining plausible deniability for ageism. It has a
secondary effect of filtering for “willing to do unpaid overtime”.

~~~
harshgupta
I think its more of aptitude test in a domain common to all programmers.

~~~
gaius
Yeah right. Tell me the last time you built your own red-black tree in real
actual code at work. Or did a sort by any other means than tacking “order by”
on the end of a query.

They are a test of how recently you crammed for your CS finals, that’s all.

~~~
hyperpallium
Would be legit if the job required tasks like implementing red-black trees or
sorts.

As is, it's more like advertising for python, then surprise java.

~~~
gaius
Indeed. If the job was implementing algorithms on bare metal then bring it on!
That job sounds like a lot of fun!

But those kinds of questions make zero sense and have zero value if the job is
actually implementing CRUD apps in a high level language. You might as well
quiz candidates on their assembly language skills too, for a Python job...

So there is obviously an ulterior motive.

------
yodsanklai
I see in the comments that some people conflate competitive programming and
technical interviews. Technical interviews (at least in companies such as
facebook and google) are usually much easier than competitive programming
problems. The problem you find on leetcode for interview preparation would be
considered beginner problems in competitions such as google code jam.

~~~
blocked_again
> Technical interviews (at least in companies such as facebook and google) are
> usually much easier than competitive programming problems.

Probably. But if you are a student outside U.S where the opportunities are an
order of magnitude less the only way to even land an interview with these
companies is by doing competitive programming. Google selects students through
APAC test in Asia where you have to be top n% in the leaderboard to even get
an interview. Doing leetcode and all would not take you anywhere in these
contests as you are competing with hardcore student programmers who have been
practicing competitive programming their entire University life in search of
an escape from the middle class. All the persons I know of in my country who
works at Google or Facebook got their job through competitive programming.

~~~
bjourne
> All the persons I know of in my country who works at Google or Facebook got
> their job through competitive programming.

Are any of them bad developers?

~~~
blocked_again
The general trend is that students do either competitive programming or
development. Most students who do competitive program hardly work on any
personal projects or open source as it is of almost zero value when it comes
to campus placements. Companies like Flipkart, Morgan Stanely, Goldman,
Amazon, Cisco etc conducts a competitive programming test as the first round
in Universities. The remaining rounds are mostly data structures and
algorithm/DBMS questions taken from GeeksForGeeks. Very few companies ask
questions about development, personal projects, open source contributions etc.
If a student does only development and hardly do any competitive programming
it is very difficult for them to pass the first round.

------
smnscu
This is a good resource that I'm currently going through to prepare for
interviews. Another one would be Competitive Programming (3rd edition) [1] but
for general interview skills the Codility lessons are also OK [2].

1 - [https://www.amazon.com/Competitive-Programming-3rd-Steven-
Ha...](https://www.amazon.com/Competitive-Programming-3rd-Steven-
Halim/dp/B00FG8MNN8)

2 -
[https://drive.google.com/open?id=1WjXxbdle0_Syip_LygBpnZkfHv...](https://drive.google.com/open?id=1WjXxbdle0_Syip_LygBpnZkfHvqST34s)

------
joshvm
I like this book, but I have some reservations for using it for interview
practice. There is no discussion on implementation of some of the algorithms,
which may be relevant in an interview (e.g. if you're not allowed to use
std::sort).

There are also a lot of topics where there's a brief overview of the
algorithm, but no code - e.g. the geometry section is interesting and has some
useful ideas, but no implementation. This gets worse as the algorithms get
more complicated, and some quite difficult topics get a cursory glance.

For the general categories of problems that you find on e.g. LeetCode or
Intervewbit, this book is really useful. It's a good, practical, companion to
a proper algorithms textbook.

Perhaps most irritating - if using this for prep - is that there are virtually
no case studies, and the case studies that are in there assume a lot of code
which isn't in the book (either boilerplate or assistance functions). This
book would be incredibly valuable if each chapter had a list of example
problems (solved or unsolved) to see how things are applied.

------
qntty
Has anybody gotten into this kind of programming post-college? Are there
communities outside of high school and college competitions for this sort of
thing?

~~~
umbs
As few comments already mentioned, post college, preparing for this type of
thing immensely helps in job interviews. I find it very hard to clear job
interviews and in 2017 alone, I appeared for ~25 job interviews. I'm very
convinced that competitive programming skills gives you a leg up in job
interviews.

Many people may already may know this, but Peter Norvig indicated that it
correlates poorly with on the job performance (at least at Google) [1]. I
wonder why, then, companies still continue to do this style of interviews.

[1]
[https://www.youtube.com/watch?v=DdmyUZCl75s](https://www.youtube.com/watch?v=DdmyUZCl75s)

~~~
chillee
Well, I think you're kind of misrepresenting his point as "competitive
programming doesn't improve your programming abilities".

It's not that competitive programming correlates poorly with job performance;
it's that, given you've been hired by Google, being a competitive programmer
correlates poorly with job performance.

Hypothetically, let's say there's 2 dimensions for a programmer's ability,
competitive programming and job experience. Each of these is distributed
independently from 0-10. You get hired by Google if competitive programming +
job experience > 10\. However, let's say that for their actual job
performance, it's 0.5 x competitive programming + 1.5 x job experience.

You now have a case where competitive programming could correlate poorly with
job performance at Google, despite having a positive correlation in general.

~~~
ctlby
Berkson’s paradox

~~~
photoJ
Do you think there is no correlation or the correlation is in fact positive? I
wondered if they every AB test with lower standards for new hires.

------
donttrack
I would watch programming contests live streams.. Are there any available?

~~~
saagarjha
In my experience, most of the time goes into coming up with an algorithm or
debugging. I don’t know about you, but I don’t think those would be
particularly interesting to watch.

~~~
donttrack
I am mostly just fascinated by people able to stream their coding. I think
most of my stream would consist of googling stuff and procrastinating on
hacker news and tabloids

------
partycoder
This book gets to the point fast. But for the fundamentals this book serves
more as a refresher than a course.

Skiena and Sedgewick both have excellent books and online courses if you need
more depth.

A nice thing about this book is that the full TeX source is on github.

~~~
partycoder
There are some sections that are a bit dense and may require some additional
work. The string algorithms section is very dense compared to the rest.

------
dang
Previous discussion at
[https://news.ycombinator.com/item?id=14115826](https://news.ycombinator.com/item?id=14115826).

------
legohead
Seemed interesting up until the "Shortening code" section, that'd drive me
mad.

~~~
svat
Another reason for shortening code in programming contests (apart from being
able to type and edit/iterate faster) is that it helps you avoid errors. For
example, in the heat of the moment and under intense time pressure (e.g. you
have finally figured out the algorithm to solve the problem, but you have only
13 minutes left to implement it or whatever), it's easy to write code like
this:

    
    
        for (int i = 0; i < n; ++i) {
            for (int j = 0; i < n; ++j) {
                ...
    

instead of what was intended (j < n).

So if you have (in your pre-written library of snippets) a macro like

    
    
        #define FOR(i, n) for(int i = 0, _n = n; i < _n; ++i)
    

then you can write:

    
    
        FOR(i,n) FOR(j,n) {
            ...
    

(the _n in the macro is for cases like FOR(i, v.size()) to avoid
recomputation) and at least avoid that particular error. Of course other sorts
of silly errors are still possible; some examples here:
[https://www.quora.com/What-is-the-worst-mistake-youve-
made-i...](https://www.quora.com/What-is-the-worst-mistake-youve-made-in-
competitive-programming-What-did-it-cost-you)

~~~
irundebian
I'm not sure that you avoid errors by forcing yourself to write short code.
With C macros.

~~~
loup-vaillant
Here's a real world example:

[https://github.com/LoupVaillant/Monocypher/commit/d7bb73f65a...](https://github.com/LoupVaillant/Monocypher/commit/d7bb73f65a852b4748097c4c7bfcf9d39ed2b7df#diff-a667a71187c131fe2449fa26edce6715L182)

So I have this function, `crypto_wipe()` that wipes memory regions with
`volatile` so the compiler doesn't optimises it away. In the link above I was
using it thus:

    
    
      crypto_stuff(stuff_ctx *ctx) {
          // stuff
          crypto_wipe(ctx, sizeof(ctx)); // BUUUG!!
      }
    

See the bug? I should have dereferenced `ctx` in the sizeof operator. As it
was, was only wiping a pointer's worth of data instead of the whole structure.
Oops.

Now I write this instead:

    
    
      crypto_stuff(stuff_ctx *ctx) {
          // stuff
          WIPE_CTX(ctx); // correct!
      }
    

The amount of repetition I avoid this way is almost negligible, but that was
enough to trigger a mistake (I had quite a lot of wiping to do). With the
macro, errors are much easier to spot (so much so that I am willing to give
100€ to anyone who finds such an error, see [https://monocypher.org/quality-
assurance/bug-bounty](https://monocypher.org/quality-assurance/bug-bounty))

~~~
irundebian
In this case the use of macros may increase the readability or assurance of
the code, still there are a lot of cases where macros can easily lead to bugs:
[https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pa...](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87151965)

~~~
loup-vaillant
Of course. You will note I only went macro to prevent an error I already made.
C macros suck, I don't use them lightly.

------
xetur
This was the first resource I used to help me prep during my most recent
interviews. Read and implement the examples yourself.

------
nojvek
Is there a printed hard copy version available as a book? I’d love to buy it.
I’m a big fan of highlighting and annotating books with a pencil.

This is a great summary to a ton of CS algorithms and datastructures. Thanks
for compiling this.

~~~
johnsonjo
Yeah the author actually came out with a physical book that he published with
springer. It’s linked here [1]. I believe it’s the same book just with extra
materials.

[1]: [https://cses.fi/book.html](https://cses.fi/book.html)

------
sigsergv
This book seems very useful for a job exams preparation.

------
tomkwong
Nice! Reading through this doc is like going back to Computer Science classes
again.

------
mindfulplay
I wonder if there is something similar to this but for pragmatic, design-
oriented challenges. Writing obfuscated code in the shortest amount of time is
akin to the people who change tires during a race car competition. I would
love to see challenges where you build the race car parts instead.

Boring but immensely more useful.

------
Fazel94
If you wonder why you need to learn competitive programming when you can just
look it up? I would like to refer you to B.F.Skinner's quote on education:
"Education is what survives when what has been learned has been forgotten."

------
sdan
Only book I used for USACO.

------
mgeorgoulo
Chapter 8.1 shows an awesome algorithm!

