The above 2D array is our maze. The zero values represent walls, the one values represent paths, and the value two represents the exit or goal of our maze.

So what are some things we must consider when trying to solve this maze? Well, we want to make sure we don’t traverse places we’ve been in a possible infinite loop and we don’t want to exceed the boundaries of our maze.

The goal here is to attempt a traversal in all directions as long as we’re on a path of one. With that said, we are just going to recursively call a traverse(column, row) function in four directions based on the current location. For each path value we land, we will change it to some other value to prevent traversing places we already visited. For these values any other that are not our goal, the traversal just dies off.

Conclusion

Like the Fibonacci sequence post I did, solving a maze is another great way to demonstrate your ability to use recursion. Please share your experiences in the comments if you’ve had a question like this in a technical interview or think you can improve upon what I’ve done.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.