What's better for competitive programming, is it Java, Python, or JavaScript?
Which language is better for competitive coding? - general - CodeChef Discuss
language agnostic - Why do programming competition contestants use C++ and Java? - Stack Overflow
What are your views on C++ vs Python (vs Java also if you use it) for LeetCode and competitive programming?
Videos
I see this being asked too many times that what is the best language for leetcode and cp. So, I want you to document here your views about which one to prefer and why. I would appreciate views of those people who have good experience with both C++ and Python (or maybe Java) but still prefer one because of some reason. I want you to state those reasons.
This is not about superiority of languages, but only about being suitable to the task at hand.
For me, I mostly use C++ and my reasons are:
-
Speed obviously. Many times even sub-optimal solution passes the test cases.
-
Rich set of data structures such as
unordered_map(HashMap),unordered_set(HashSet),map(TreeMap),set(TreeSet),stack,queue,dequeetc, almost anything you can imagine. -
A truck load of algorithms - I know python has very good stuff in
itertoolsandfunctoolsbut C++'s#include<algorithm>is just another beast. Many leetcode problems become one liner that is readable and can be used even in production code (actually it should be used in production code). Need nth largest or smallest element -std::nth_element, need to merge two sorted ranges -std::merge, need to rotate the array by kth places-std::rotate, want to partition the array -std::partition, want to partition but the initial order is also important -std::stable_partition, want to sort but initial order is also important -std::stable_sort, that's just scratching the surface. -
Tail recursion elimination - I can write recursive implementation which is easier to reason about and the compiler will turn it into iterative one for me. SegmentTree and Trie becomes a joke using recursion. Sometimes it really matters, in this weekly contest (Leetcode 311) I solved 4th problem using recursive Trie solution, since I finished before time so thought that I should try it in Python also for practice but I could not get it to accept until I converted everything to iterative.
-
std::string_view- It makes recursive solutions involving strings really easy - you don't need to copy string so there's no memory penalty and performance penalty. C++20 also hasstd::spanwhich brings the same ease also tovectorandarray.
I do come to python for some problems for which my reasons mostly are
-
DP problems become a joke using
lru_cacheandcache, actually most of the DP problems I first solve in python, because I can write it like that the maths formula, which helps me figure out what values I need to store in DP table. -
The ease of putting anything (constant data structures) into
setormapis very convenient as opposed to writing the hash function forstd::unordered_mapandstd::unordered_set. -
Arbitrary precision numbers, for problems involving huge numbers there's no better solution that using Python.
Also give response in the poll about which one you use.
I know these languages are odd choices (especially JS) because the most common one to use is C++. I picked those 3 languages because they're the one I'm most comfortable with. For whatever reasons, I don't feel like I like C++ so I cross it out.
So, out of those 3 languages, which one is better for CP? I don't really intend to join a contest tho. This is mainly for job interview and grinding Hackerrank, Lettcode, and Codeforces.
Great question! As someone who has dabbled in programming contests a bit myself, I may have something to say.
[Let's get the standard disclaimer out of the way: contest programming is only loosely related to "programming in the real world", and while it tests algorithmic and problem-solving skills and the ability to come up with fast bug-free working code under time pressure, it does not necessarily correlate with being able to build large software projects, write maintainable code, etc (beyond the fact that well-structured programs are easier to debug).]
Now for some answers:
C++/Java are more common than other languages in the real world as well, so you'd expect to see a higher proportion anywhere. (But it's even higher in the contest population.)
Many of these participants are students, or got into contests as students, and C++/Java are more common "first languages" that students learn. (Undergrad students these days may start with Scheme, Haskell, Python, etc., but high-schoolers (often self-taught) less often.) In fact, many of the Eastern European participants still use Pascal, and are more amazing with it than the rest of us will ever be with any language.
The school- and college-level contests usually use these languages. The International Olympiad in Informatics (IOI) allows only C, C++ and Pascal (or maybe it allows Java now; I haven't kept up), and the ACM Intercollegiate Programming Contest (ACM ICPC) allows only C, C++ and Java. TopCoder allows C++, Java, C# and VB (really :p); and recently, Python. So you could say the "contest ecosystem" has more C++/Java programmers in it. Google Code Jam and IPSC are among the few contests that allow code in any language, actually.
Now the question is, in GCJ where the contestants are free to choose a language, why wouldn't they choose Python or Scheme? The most relevant factor is that these languages are slow. Sure, for most real-world programming they are easily fast enough, but for the tight loops that are often involved in getting a program to run under the n-second limit for all test cases, these languages don't cut it for any of the algorithmically more involved problems. (A problem designed to accept O(n log n) solutions but not ฮ(n2) solutions for C/C++ frequently rules out even optimal O(n log n) solutions in slower languages. Even Java used to be given a handicap at USACO; I'm not sure this is still the case.)
Another factor is the libraries: C++ and Java have better libraries for frequently useful algorithms and data structures (e.g. red-black trees, C++'s next_permutation), while Python's libraries (good enough for the real world) are less useful here, and Prolog and Scheme... I don't know about their libraries. This is a relatively minor factor, because these programmers can write their own code when necessary. :-)
General-purpose multi-paradigm languages are more useful for just getting things done within the time constraints of the contest, than languages that force a philosophy or way of doing things on you. This is why Prolog will always remain unpopular, for instance. (General philosophy: some languages are "enabling" languages that let you do anything including shooting yourself in the foot, some are "directing" that force you to do things the right way.) This is also why C++ is three times more popular than Java in the general contest participants, and much more popular among the top contestants. Since code doesn't have to be read by anyone else, it's ok and even useful to have loop macros like
FOR(i,n)(less code to type, and more importantly less chance of making a bug when in a hurry). Nothing against Java, there are a few top programmers who use Java too. :-)Finally, although many of these top programmers may have C++/Java/Pascal as their "first language", they are not good because of their language, so you don't have to despair about that. Many of these same programmers have won contests like the ICFP contest even with intentionally using crazy languages like shell scripts, m4 (used in autoconf), and assembly (the team named "You Can't Spell Awesome Without ASM").
I liked Jerry Coffin's idea of plotting contestants of the Google AI contest, so I took all of the results and plotted them (calculated mean, standard deviation, and then graphed the normal distribution curves in Excel).
With Lua and JS, got this:

Without (there were few contestants, so maybe the results are skewed):

It looks like Java participants did markedly worse than the rest, while Go, Common Lisp, and C are on the better end.