Ad
  • Custom User Avatar

    Really the question you need to ask yourself is, "will recursion actually save me time/space for this problem?"

    Recursion in most languages involves saving a set of details onto a call stack, then once we've gotten to the base case, popping them all off the stack one-by-one and applying any remaining logic.

    A mental picture could be sending a package through mail, only for n number of addresses saying, "oh, this isn't your final destination. Here: go to this address instead." Eventually, the right address gets the package, but then simply scribbles a note on the box and ships it back to the last address that sent the box to it! This then daisy-chains its way back to the post office, before its done.

    Recursion is a neat tool in programming, but often times its way more efficient to not go through the hoops of a postal service-esque solution if you know in advance what optimizations you can make space/process-wise to get your solution.

  • Custom User Avatar

    Thanks for your answers. I was trying various methods for solving this problem. And wondered about recursion.

  • Custom User Avatar

    Depends on language, whether it has TCO or not, for this kata recursion isn't doable for js, ruby, python, if done correctly should work for elixir.
    Edit: Don't know about rust.

  • Custom User Avatar

    NOTE: There will also be lists tested of lengths upwards of 10,000,000 elements.

    Recursion isn't a good idea here.

  • Custom User Avatar

    Is there anyone who could make recursive solution for this case? I have tried but getting RecursionError, because of the limit.