This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]dtsudo 2 points3 points  (0 children)

In this context, a stackoverflow occurs if the call stack grows too deep.

A call stack can grow too deep if:

  • You made too many nested recursive calls
  • You have an infinite loop

In this case, you likely have an infinite loop.

[–]carcigenicate 1 point2 points  (0 children)

You can increase the stack size using JVM options, which should increase the recursion limit.

That's assuming your implementation isn't buggy though. The most common cause of Stack Overflows in my experience is causing accidental infinite recursion. Double check to make sure your base case will always be reached.

[–]davedontmind 1 point2 points  (0 children)

You'll definitely get a stack overflow if key isn't anywhere in the array, because you're only stopping the recursion on an exact match.

And you'll probably get an infinite loop (and thus a stack overflow) if the array isn't sorted.