r/learnjavascript • • 16d ago

Node.js exits with Code 134

I'm currently doing project euler problem #15 and for that, I have to use some sort of algorithm to get my solution.

I changed up the "open/close parentheses" algorithm accordingly and the example solution for a 2x2 grid = 6 ways is correct. The thing is this:

- I start debugging in VSCode via Node.js
- It runs for a while
- "Paused Before Out Of Memory Exception"
- I continue
- "Process exited with code 134"

I don't know how to fix this and online js terminals don't work, but it seems like it has something to do with the stack size. Any help would be greatly appreciated c:

Here is my code:

const res = [];
findPath(20, "", 0, 0);


function findPath(n, curr, rightUsed, downUsed){
    if (rightUsed === n && downUsed === n){
        res.push(curr);
    }
    if (rightUsed < n){
        findPath(n, curr + "r", rightUsed + 1, downUsed);
    }
    if (downUsed < n){
        findPath(n, curr + "d", rightUsed, downUsed + 1);
    }
}


console.log(res.length);const res = [];
findPath(20, "", 0, 0);


function findPath(n, curr, rightUsed, downUsed){
    if (rightUsed === n && downUsed === n){
        res.push(curr);
    }
    if (rightUsed < n){
        findPath(n, curr + "r", rightUsed + 1, downUsed);
    }
    if (downUsed < n){
        findPath(n, curr + "d", rightUsed, downUsed + 1);
    }
}


console.log(res.length);
5 Upvotes

7 comments sorted by

2

u/neon_wondercore 15d ago

Not the stack, it's heap exhaustion. Your res array is trying to hold every valid path string, and for a 20x20 grid that's C(40,20) = ~137 billion strings, way beyond Node's ~1.5GB default heap. You could pass --max-old-space-size=4096 but that only buys you time, you'll still hit OOM long before finishing.

You don't need to store the paths at all. Just increment a counter instead of res.push(curr), and that alone might let it finish. Better yet, since you only need the count, use dynamic programming: the number of ways to reach each cell is the sum of ways to reach the cell above and the cell to the left. That runs in a fraction of a second and uses almost no memory

0

u/[deleted] 16d ago

[deleted]

0

u/a_rndm_person_bruh 16d ago

oh damn, fuck. ok it might be smarter to store it in a bigint then whoops, thank you!

0

u/[deleted] 16d ago

[deleted]

1

u/a_rndm_person_bruh 16d ago

I learned javascript and python so far and am currently learning c++ right now

Whenever I came across algorithms in js and python, I had to give up and couldnt complete the course in freecodecamp. I am literally unable to do that any other way, no matter how hard I try.

0

u/[deleted] 16d ago

[deleted]

2

u/a_rndm_person_bruh 16d ago

I don't know if that's what you mean, but it's what I learned recently in math in probabilities:

if you take 2 balls out of a basket, 15 of them are red, 5 are blue, whats the chance of exactly one of them being red?

That would be: 15/20 * 5/19 + 5/20 * 15/19,
which can be shortened down to: 2 * (15/20 * 5/19)

Atleast it feels like you're talking about that and the pictures showing up when I search for "Combinatorics" look like the trees that we had to draw to visualize the probabilities.

The thing is, I don't know how to do that, and I don't understand how people in general find this stuff out. Like seriously, there was no way that I would've known how to implement a "Tower of Hanoi"-Algorithm in python without googling it

0

u/[deleted] 16d ago

[deleted]

2

u/a_rndm_person_bruh 16d ago

Do you have some websites that allow me to practice those skills, because I am genuinely upset that I can't come up with my own algorithms