Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Why does concatenating this string before adding it as a parameter to a recursive function cause stack overflow?
function permToN(charList, returnLen, base) {
  if (base.length === returnLen) {
    console.log(base)
  } else {
    for (let char of charList) {
      base += char
      permToN(charList, returnLen, base)
    }
  }
}

permToN(['a', 'b', 'c'], 2, "");

Hi was brushing up on some algorithms and this baffled me. In the above code the problem errs out with Maximum call Stack size exceeded.

However - if I remove the line base += char and make the recursive function call instead with permToN(charList, returnLen, base + char) concatenating the string inside the function param the function works as intended - no stack overflow I am pretty baffled why that small change causes stack overflow. I think I may not understand how the memory is functioning under the hood ? Would greatly appreciate anyone who can explain difference in performance btwn the functions. Below is the function with the concatenation occurring in the params of the function call which works as intended. The above function causes stack overflow. Do not understand the fundamental difference here. thank you!

function permToN(charList, returnLen, base) {
  if (base.length === returnLen) {
    console.log(base)
  } else {
    for (let char of charList) {
      permToN(charList, returnLen, base + char)
    }
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!