Prepin
Log in

Determine the total count of distinct paths in a maze from the starting point to the endpoint.

Approach / Explanation

This question is commonly posed in software engineering interviews and tests a candidate's foundational knowledge of graph traversal algorithms and dynamic programming. The scenario describes a theoretical maze represented by a 2D matrix or grid, where each cell indicates either a passage or a wall. The goal is to identify all unique paths from a specified starting point (source) to a target point, with movement restricted to certain permitted directions (usually right, left, up, and down, though diagonal movements may be included in some instances). Unique paths mean that retracing a path with the exact sequence of steps counts as a single path. To tackle this question: Outline the algorithm you would employ to address the problem, such as depth-first search (DFS) or breadth-first search (BFS). If the maze is extensive or contains numerous passages, consider using dynamic programming to enhance your solution's efficiency. A crucial aspect to remember is to avoid cycles (visiting the same cell multiple times) in your approach.

Suggested Answer

A common approach to this issue is to modify the depth-first search (DFS) algorithm. The concept involves navigating through the maze from the starting point to the destination. At each cell, we have four possible directions to move: right, up, down, or left. If a path successfully reaches the destination, we will increase our path count. While transitioning from one cell to another, we temporarily mark the original cell as inaccessible to prevent revisiting it, effectively avoiding cycles. After exploring all possible paths from a specific cell, we backtrack and restore access to that cell. Throughout this entire process, we maintain a tally of the unique paths found, which ultimately gives us the total number of distinct paths in the maze from the source to the destination.

Alternative Answer

Another approach to tackle this problem is through dynamic programming. The concept of dynamic programming here involves storing the solutions to sub-problems and utilizing them to address the overall problem. This method proves to be more efficient, especially when dealing with larger maze sizes. We begin at the destination point. For each cell, we determine the total number of unique paths to the destination by taking into account all adjacent cells from which we can reach the current cell. We can record the number of unique paths from each cell to the destination in a 2D matrix that matches the size of the maze. Ultimately, the cell corresponding to the source will contain the total number of unique paths leading to the destination. The time complexity of this technique is O(n*m), where n and m represent the dimensions of the maze, making it quite efficient for larger mazes. It's important to note that when employing this method, we should initialize the destination cell with a value of 1 (indicating there is one unique path from the destination to itself), while the cells from which the destination cannot be reached should be set to 0.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.