This week, there was a question on Meta Stack Overflow about the right way to ask Competitive Programming questions on Stack Overflow. To the uninitiated, Stack Overflow might seem like a good place to ask questions about Competitive Programming. It’s the standard place on the Web to ask programming questions, and competitive programming is about programming, right?
Stack Overflow vs. Competitive Programming Questions
Actually, it’s not quite that simple. First of all, as many beginners have learned the hard way, Stack Overflow users are very particular about how they want questions to be asked. But the nature of competitive programming questions makes it difficult to ask them in a Stack Overflow-approved manner. Stack Overflow is optimized for real-world questions, and participants are accustomed to questioning the question: asking why the questioner needs to know what they are asking, and whether they should be asking a different question. For competitive programming questions, this doesn’t make much sense. The question exists for its own sake, like a homework problem or an interview question (which are two other controversial question categories on Stack Overflow).
Secondly, and perhaps because of that real-world bias, some high-rep Stack Overflow contributors have a kind of allergic reaction to Competitive Programming. Here’s part of the answer from user πάντα ῥεῖ on this week’s meta question:
I tend to DV/CV [down-vote/close vote] almost instantly anything here when I see “SPOJ”, “CodeChef”, “Project Euler”, etc. mentioned in a question. Usually these questions can’t be answered reasonably from the given input. Missing test cases and debug efforts.
I really puke on that unuseful stuff appearing here, as much as I think these Online Code Judge engines are just a complete waste of time for someone who seriously wants to learn a high level programming language like c, c++, java, c# or alike.
And part of a comment from Hans Passant, the #4 user on Stack Overflow by reputation:
Any kind of “help me cheat the system and win Internet points” question is going to be received poorly anywhere, that’s just not a good enough reason for anybody to invest their free time.
With these types of combative response, is it even worth asking Competitive Programming questions on Stack Overflow? Sometimes.
Stack Overflow and Stack Exchange
Stack Overflow questions work best when they include a small amount of code, and a focused question about that code. For example, here are some questions I have found helpful when coding Java solutions to UVa problems:
- Getting unexpected type error when declaring new generic set: Java Generics don’t work with primitive data types.
- Why does Map.put() overwrite while Set.add() does not?: These interfaces are intended for different purposes, and this is reflected in the behavior of types that implement them.
- How do I copy a stack in Java?: Use
clone()
, get an unchecked cast warning, then read the answers to this question to find out what to do about it.
To solve competitive programming problems, you’ll need a good command of your language’s standard libraries. Collection classes are commonly used in competitive programming to store and retrieve the problem data efficiently. The three questions above cover details and quirks of a few Java collection classes.
Unless you’re using an unusual programming language, you’ll usually find that your question is already answered in some form. To avoid posting duplicate questions, it’s good to practice skimming Stack Overflow answers and applying them to your specific situation. Fortunately, the top answers are often written more clearly than the official documentation.
If your question isn’t about code, then another Stack Exchange site may be useful. Mathematics Stack Exchange accepts questions about “math at any level,” including the math that’s required to solve some competitive programming problems. But as with Stack Overflow, it’s best to formulate your question in terms of the math help that you need, rather than “here’s a puzzle I’m trying to solve.”
There’s also a Stack Exchange site called Programming Puzzles & Code Golf, which says it’s “for programming puzzle enthusiasts.” This sounds like a perfect place to ask competitive programming questions, but it turns out that they like to ask and answer their own programming puzzles, not discuss puzzles from other sites.
Quora
If you have a general question about competitive programming, or a question about a specific contest problem, then Stack Exchange sites are not ideal for getting an answer. However, Quora has a topic that accepts almost any question related to competitive programming. For example:
- General: How is competitive programming different from real-life programming?
- Specific: How do I solve UVa 11450 (wedding shopping) using DP?
Quora culture has evolved to be tolerant of most questions. Although there are facilities for downvoting, closing, and merging questions, they aren’t used nearly as assiduously as they are on Stack Exchange. Although this has a negative effect on question and answer quality, it has also encouraged some good content that would never make it on Stack Exchange. In particular, a competitive programming community has evolved on Quora, attracted top competitive programmers, and provided a lot of useful questions and answers.
Do You Need to Ask?
So you have a few Q&A options depending on the nature of your question. But it’s also important to consider whether posting a new question is the right course of action. That decision will partly depend on the type of competitive programming question you’re considering.
General questions (Quora)
After a few years of activity, the Competitive Programming topic on Quora has a wide range of questions. So it’s rare that I see a truly original new question there anymore. My recommendation for general competitive programming questions is to read through the top Quora questions, and only post a new one if you have something new to add. If you just want to hang out and chat about competitive programming, there are better options, like the TopCoder forums.
Coding questions (Stack Overflow)
Since competitive programming solutions rely on the basics of a language, your question has also probably already been answered on Stack Overflow. The same rules apply about reading the existing questions, except that Stack Overflow users are more hardcore about closing duplicates than Quora users are.
Questions about a specific contest problem
This category of question is more tricky. As discussed in the Meta Stack Overflow question at the top of this article, Stack Overflow is not the place to ask about specific programming puzzles. They just don’t like them. You can certainly ask them on Quora, and you’ll usually get an answer.
But I take a different approach to this category of question. Rather than pick an arbitrary problem to solve and then try to find an answer when I get stuck, I prefer to work on problems that I know are likely to have hints and solutions available. In my experience solving the uHunt starred problems, I have never had trouble finding resources. There are always hints, sample uDebug test data, and complete solutions available.
For your first few hundred practice problems, there’s not much benefit to seeking out newer problems that might not have anything written about them. You have to get familiar with the standard algorithms and techniques one way or another, so you might as well use older problems that have explanations. The uHunt problems fit that description, and there are problems on other popular online judges like SPOJ that people have also written about.
Even with resources available, there’s still work involved in understanding a problem. When I write an editorial for a hard problem like UVa 11402, I include a lot of detail. But I still expect that it would take an hour or two of study for a reader to understand the solution. That’s a lot of work, but it’s better than being completely stuck!
Write Your Own Manual
There are a lot of competitive programming resources out there to answer your questions. But when it comes to solving practice problems, I would recommend this approach:
- First, try everything you can think of to come up with your own solution. (Here’s the process I use for every problem). Even if you don’t get the solution, struggling with it will solidify the problem’s ideas in your mind.
- If your problem is on uDebug, see if you can reverse-engineer a correct solution using sample test data that someone has posted, or that you generate yourself. In other words, compare the accepted results that uDebug gives you with the results produced by your code, and diagnose any discrepancy.
- If you’re still stuck, Google the problem ID. If you’re using older problems from a well-known source, you should get some hits.
- Study the hint or solution that you find, and see if you can adapt your solution now that you know how someone else approached it.
- Write your own manual: Describe in your own words the key ideas for solving the problem. I don’t do this for every problem, but I do a write-up for the ones I find interesting. It’s a good way to make sure you really understand the solution.
(Image credit: walknboston)