Using JavaScript to Find a Path Between Two Cells in a Grid

D Bharathi
codeburst
Published in
5 min readJul 13, 2020

--

This is definitely one of the most commonly asked questions in any programming interview! The funny part is, the question is often twisted and asked in many ways. But, the foundation for the solution remains the same. It would be a variant of Breadth-First Search (BFS).

Many a time, young programmers are daunted by the idea of implementing BFS and finding that perfect solution. But, the use of this algorithm does not have to be very scary. In fact, it is much simpler than you think.

Here is a simple YouTube video to help you with how this problem statement can be cracked using BFS.

Solving the Problem!

Pseudo Code

  1. Find the size of the grid
  2. Store current location details, temporarily.
  3. Create a queue, push the location of cells that are not visited.
  4. Check if the current location matches the destination. If yes, return the current location.
  5. Find neighbors for the

--

--