We routinely allow questions where the code works correctly for small inputs but fails to scale to larger inputs. The and tags are often applied to such questions.

What if the problem is that the stack sometimes overflows? That seems to be the premise of this question.


4 Answers 4


To an extent, yes. In this particular case, no.

I don't see a problem with allowing questions that have Stack Overflow Exceptions, provided the code works acceptably on a smaller input set. If we were to eliminate questions with SOE, then by all rights we should eliminate questions as well, simply because they "don't work as expected" for the very specific large input they are failing on.

A Stack Overflow Exception is just another variant of . Except, instead of the time being exceeded, the actual amount of information on the stack has been exceeded. The only difference is the manner in which the error with the code is presented.

SOE questions can also help us all out: now we have a chance to point out potential optimizations that can lead to tail-call recursion which wouldn't overflow the stack, or reduce the memory footprint to reduce stack space, et. al. It gives all users a chance to learn how to prevent their own Stack Overflow Exceptions.

The reason I would say "no" to this question, is that the OP doesn't want a code review, but instead wants:

In some test cases it is giving StackOverflow Error. Could I please help me correct this code?

Were the question rephrased, it would be a fine fit. Were the OP to be looking for a general code review with the possibility that it would fix the SOE, then it would be fine. As it stands, the OP specifically wants a fix to the SOE.

If the code works for small inputs, then we can assume it would work for large inputs, were the SOE not present.

  • \$\begingroup\$ The quote this person gave could easily just be their rough english, to them "correct this code" could be "review my code thoroughly". I think this possibility is why we shouldn't lean too heavily on someone's wording to determine whether a question is on topic. \$\endgroup\$ Aug 8, 2016 at 8:33
  • 1
    \$\begingroup\$ @SuperBiasedMan Aye, that could even be the case. In any instance, I feel that a StackOverflowException shouldn't be an immediate VTC. \$\endgroup\$ Aug 8, 2016 at 15:50


This question resolves around the bug he currently encounters. This is not a question asking for a review about any and all aspects of the code.

If a user posts a question on Code Review, we assume it's working to the best of the authors knowledge. Sometimes there's edge cases in which it is not. Sometimes the author is aware of those edge cases and mentions them. In that case we simply exclude those cases from the specification, making the rest by default 'working code'.

The code in this specific question is flawed. The author knows this. This is a Stack Overflow question, quite literally.

I would advice the author to read up on how to write a Minimum, Complete and Verifiable Example and post a question on aforementioned site.

The main difference between a Stack Overflow question and a is the latter will still work. It might take half a year to complete, but it works. A question which overflows a stack as a major design flaw and will not work for the required input. Simply put, it's broken.

  • \$\begingroup\$ But time-limit-exceeded questions often never run to completion for large inputs, and also require major redesign. Why do you believe we should treat them differently? \$\endgroup\$ Aug 8, 2016 at 0:12
  • \$\begingroup\$ Similarly, a stack overflow question would run given a machine with a large enough stack. \$\endgroup\$ Aug 8, 2016 at 13:13
  • \$\begingroup\$ Not taking the size of the stack into account will make you run into crashing code. Code that's simply taking to long is not crashing. I'd say there's a major difference between the two. In practice you can't use any of them, but a crash is a crash and running without crashes while simply taking too long is an optimization problem. \$\endgroup\$
    – Mast Mod
    Aug 8, 2016 at 14:33

This has everything to do with what the question is about. If the question is about how to optimize a program to reduce memory consumption, it is fine. If the question is about trouble-shooting code that causes stack overflows, it belongs on the Stack Overflow site.

On desktop/hosted systems, stack overflow is almost always caused by either using ridiculously large local variables that should have been allocated on the heap, or by silly uses of recursion.

In such cases, SO has canonical duplicates that can be used to instantly close the question, those kind of questions have been asked thousand times before. Example.

On-topic examples:

  • How do I reduce the memory consumption of this working program?
  • How do I optimize this working program, which is using recursion?

Off-topic examples (belongs on SO):

  • I'm getting a stack overflow in this program, what could be cause?
  • The program works until I invoke this specific recursive function, why?
  • Questions about how to configure, memory map or diagnose the stack.


my Comment really points out the issue here.

when you say The same code if written in C/C++ has passed all test cases. that makes me think that there is something different with garbage collection, maybe there is something that is different about Java that you don't have in C or C++ or maybe you have coded it correctly in C/C++ but incorrectly in Java. I would double check your code to make sure that it is doing the exact same thing as the code you wrote in C/C++

The code that was written for C and/or C++ works, but the code written in Java fails some of the test cases, because there is something different about the Java code it doesn't meet the requirements.

The purpose of the tests is to make sure that the code meets the requirements, and if the code fails to meet the requirements then it is not working the way it is supposed to, it is broken.

The same requirements were passed in the C/C++ code, I would say that the settings for the Stack and the Time Limit wouldn't change from one language to the next in terms of the challenge, so I think that this means there is a fault in the code itself, that the code isn't doing what it is supposed to do.

Time limit exceeded is different than Stack Overflow Exception.

Stack Overflow Exception will break the application, where the time limit exceeded isn't truly an exception, it is breaking an arbitrary rule to the coding challenge that most likely won't break the application.

  • \$\begingroup\$ The stack size limit is tunable when invoking the JVM, so it is also an arbitrary limit. \$\endgroup\$ Aug 10, 2016 at 12:51
  • 1
    \$\begingroup\$ @200_success That's not arbitrary. You can increase the hard limit, but you can't tell the JVM to 'suck it up', like you can an end user. \$\endgroup\$
    – Peilonrayz Mod
    Aug 10, 2016 at 13:02
  • \$\begingroup\$ it still breaks the application, where the time limit exceeded doesn't break the application \$\endgroup\$
    – Malachi Mod
    Aug 10, 2016 at 13:02
  • \$\begingroup\$ But what if the time-limit exceeded is what prevent the application from hitting a let's say stack overflow ? \$\endgroup\$
    – Marc-Andre
    Aug 10, 2016 at 14:43
  • \$\begingroup\$ Then the time limit exceeded would be a feature of the application to keep the exception from being hit and the stack overflow wouldn't be the issue, @Marc-Andre. \$\endgroup\$
    – Malachi Mod
    Aug 10, 2016 at 16:24

You must log in to answer this question.

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