1

The usual expectation when asking for debugging help on Stack Overflow is a Minimal Reproducible Example, including any program input needed to reproduce the issue, and the exact text of compilation or runtime error messages.

However, sometimes new users come here looking for help with code that they are writing for a coding challenge (leetcode, hackerrank, etc). In such cases, they might have trouble creating a minimal reproducible example:

  • Their code might not constitute a complete program, because the challenge might be just to write a single function. (The challenge site might execute it within some testing harness that is not provided to the user.) So they may not have a main function, complete definitions of data structures, etc.

  • They might not have access to the exact input data that causes the failure, because sometimes these sites use secret test data.

  • They might have trouble finding out how the program is failing, because the site might use a compiler or interpreter with unhelpful error messages, or just indicate success or failure without further info.

  • They might not have development tools for their language installed locally; they could be relying on just editing and submitting code directly to the site.

  • They might not even be able to use printf debugging, because they might not get access to their program's output (to prevent them from recovering the secret test data).

Such questions sometimes get into a pattern of: New user posts their attempted submission; post gets closed because it isn't an MRE; new user gets frustrated because they don't have the things that an MRE asks for, and don't know how to produce them; experienced users try to explain in comments, but lose patience because they've explained the same things hundreds of times before on similar comments; things get testy; new user leaves in a huff and thinks SO is terrible.

What are some more helpful pieces of advice for new users in this situation, that may help guide them toward writing a question that can be well received and answered? Then we can point them to this meta post, rather than trying to rehash the same points in comments every time.

If possible, please try to write answers that will be friendly and accessible to newcomers. New users in this situation may be unfamiliar with the SO model and community norms; may be inexperienced with the programming language they're using; and may be unfamiliar with basic concepts and techniques of software development and debugging.

(Let's set aside the question of whether it is ethical to request or provide assistance with such problems and assume that we are treating such questions like any other question.)

9
  • 13
    Their code might not constitute a complete program, because the challenge might be just to write a single function - I don't see how that's a problem. Depending on the language, a single function is just fine for a programming problem.
    – aynber
    Commented Jun 19 at 20:10
  • 10
    I sympathize with dealing with new user / new programmer questions, especially in the context of poor SO Inc onboarding & documentation, but it's not clear to me how this competition case/"pattern" is meaningfully different from any other debugging/[mre] case or how an answer to it is different from a generic [mre] explanation.
    – philipxy
    Commented Jun 19 at 20:11
  • 4
    @aynber: Okay, but for other languages not. I'm thinking for instance of stackoverflow.com/q/78627330/634919, where the language is C (in which a buildable runnable program must have a main() function), and the code written by the asker consists only of an addTwoNumbers() function. Sure, it's a valid problem specification to say "write an addTwoNumbers() function", but it's not reproducible: if I want to test their code to try to find the problem, I'll have to write my own main function to call theirs, as well as constructing appropriate input objects. Commented Jun 19 at 21:38
  • 4
    @aynber: And the whole point of requiring an MRE is that an answerer shouldn't be expected to do that. Commented Jun 19 at 21:39
  • 7
    For many questions, judge challenges especially, not having a main is a dead give-away that no effective testing and debugging took place and, regardless of whether or not the question is answerable from the given snippet, the voting will reflect this. Never discount the social importance of showing your work. Commented Jun 19 at 22:28
  • 4
    @philipxy: It's not about how the answer is different; it's the difference in what the asker needs to do to make the question answerable. For a normal debugging help question, the asker actually has a complete program and test input in hand (even if they only posted a fragment of it), and just needs to extract the right pieces to make an MRE. For a coding challenge, they may only have the fragment, because that's all they were asked for, and so will need to actually write nontrivial new code in order to have an MRE. Commented Jun 19 at 23:04
  • 4
    @philipxy: In stackoverflow.com/q/78627330/634919 which I linked above, I asked OP to include the main function, headers, etc. They responded that they didn't have any of those because leetcode didn't require it, and didn't seem to understand that this wasn't a satisfactory answer. I didn't have the patience to explain to them why not, and what they needed to do, but if there'd been a post like this to point them to, it could have done them some good. Commented Jun 19 at 23:07
  • 4
    It is kind of a catch 22 situation. If you fit in the list of bullet points... you should fail the challenge. That's the whole point of a challenge - to prove you can do it (and hopefully have fun while doing it). Usefulness remains one of the main quality indicators for Stack Overflow and a question like this will struggle to meet that demand in the best of times. If it were me, SO would be the very last place on earth where I would post such a question.
    – Gimby
    Commented Jun 20 at 14:19
  • One could maybe compare code challenge environments to proprietary, closed source software that only links to user provided code in some way. Either both is ontopic or none and the treatment should be the same. Commented Jun 22 at 17:56

2 Answers 2

10

What seems important to me is that one should not expect the readers of the SO question to know anything about the code challenge site. They should not need to visit that site, let be that they should create an account there. The reader should get enough information to copy the provided code into their local environment, execute it, and reproduce the problem -- without the need of ever visiting the code challenge site.

Addressing the one who posted (or wants to post) a code-challenge related question: here are some tips for creating a reproducible example:

  1. Make the code run in your local environment.

    For that you may need to add code in your local environment that was not necessary on the platform. The platform might not give you access to the code they use to initialise the test case input and to pass it on to your solution code. But is a good idea to write this code, so that you can run your code in a local set-up with some test input, and use all the power of your local environment (like debugging capabilities). If you don't know yet how to do that, it would be a good opportunity to research this further before posting a question. The code you need to add depends on the programming language, on how the code challenge site expects you to provide the solution, and on the data types specific to the challenge. But once you have it, you'll be able to re-use at least some of it for your next code challenge.

  2. Try boundary cases.

    Pay attention to the constraints of the code challenge. Often there will be a "constraints" section that says something like 1 <= n <= 109. It would make sense to prepare inputs that are at the boundaries of these constraints, and see if that produces an issue.

  3. Compare with a brute force solution

    If you can think of a non-efficient (brute force) solution, but of which you are confident it produces correct results, then also implement that one. Make sure to test it thoroughly. Then create random inputs and run both solution codes on these inputs and compare their results. Repeat this until you find a case where the results are different, and analyse which of the two are correct, if any. Then try to reduce the input and still get this anomaly. With this information you can reduce the code to just run with this particular input and post it as a minimal reproducible example.

Some things to be aware of specifically in the context of such online code challenge platforms:

  1. Be careful with global/static variables.

    This includes any other state that outlives a single execution of your solution code. Code challenge sites will execute your solution multiple times with different inputs, and such variables may not be reset to their initial values, negatively impacting a next test case. Think of structures you might use for memoization, or counters, sums,...

  2. Understand the input/output data types.

    The code challenge description might describe input examples in a text format that is not actually the data type that your code will receive as input. For instance, it might be that your code must be a function that receives the head node of a linked list, while the description gives examples of such linked lists as

     [1, 2, 3, 4]
    

    This is mostly a concern for weakly typed languages, but don't mistake this format to mean that your code receives an array or JSON as input. The same goes for expected outputs. The challenge description might give examples of expected output in this same JSON-like format, while in reality your code is supposed to return a linked list, binary tree, or other data structure.

-3

I'm a little surprised to see somebody suggest that a main method helps make something reproducible. How to make a question more understandable? says (emphasis added):

A simple MVCE that can be copy/pasted into a main method is the best, but should make it as easy as possible for any random strange[r] to compile some code and see the issue.

The main method is NOT need for reproducibility. If anybody fluent in the language can add the main method boilerplate, the "minimal" in MCVE means that it should be left out. We shouldn't be closing the question for information that could easily be edited into it by the community. We should be closing questions for critical omitted information that only the asker could know.

I'd suggest that guidelines for posting about coding challenges should be little different that posting about any other code debugging:

  1. Post the code you have that fails, omitting any irrelevant portions.
  2. State what the problem is. Give error messages if there are any, otherwise explain the unexpected behavior.
  3. Let people know what environment you are working in if that could make a difference.
  4. If there are hidden test cases, or unhelpful messages, give as much info as you can. For example "It says there are 6 failing test cases which it hides from me. I tried the following three test cases and they all pass. I'm not sure what additional test input would cause a failure."
2
  • 5
    But do note that If people Can't reproduce the problem by giving a simple entry point and adding a few headers or whatnot, they will let you know. Often with down and close votes. Also note that every change a potential answerer needs to make to the example to run and reproduce the problem is an opportunity to change the program behaviour for good or bad. Accidentally fixing the mistake or reporting on a mistake the answerer inserted doesn't help anyone. Plus, the stranger the problem, the tighter the rails you, as an asker, want on the example to lead readers to the problem. Commented Jun 21 at 18:25
  • 4
    Having a main method may not be necessary, but having all the information such that writing a main method is trivial is. For beginners unused to a language, the simplest rule of thumb to make sure they have all of that context is to write that main method. Commented Jun 22 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .