0

Copy before you push

Recursionused in ~80% of apps

The bug that catches everybody exactly once. Your backtracking is correct, your logic is right, and every answer comes back empty. This lesson exists so that happens to you here rather than in an interview.

Core idea. An array in JavaScript is a reference, not a value. Storing the path stores a pointer to the one array you keep changing, so every stored answer is the same array and all of them end up empty. The fix is to store a copy, and the reason is worth understanding rather than memorising.
Playground, this isn't a screenshot
every subset of [1, 2, 3], one move at a time
the path you are holding
empty
recordrecord a copy of []
move 0 of 22adds 0 · undos 0
answers recorded so far
[]

Count the adds and the undos as you go. They always end up equal, and that is what backtracking means.

↑ play a little first, then dig in