Building on the "Maze" series, "The Maze III" introduces a hole on the board. The ball still rolls until it hits a wall, but if it passes over the hole, it falls in and stops immediately. Your goal is to find the path that leads the ball to the hole with the shortest total distance. If there are multiple paths with the same minimum distance, you must return the one that is lexicographically smallest (e.g., "d" for down is smaller than "l" for left). This problem adds both a unique stopping condition and a tie-breaking rule for the path description.
This the Maze III interview question is a high-level challenge often used by Google. It tests a candidate's ability to handle multiple optimization criteria (shortest distance and lexicographical order) simultaneously. It requires a robust implementation of Dijkstra's algorithm where the state includes not just the distance but also the path taken. Interviewers look for clean state management and the ability to correctly modify the "rolling" logic to account for the hole.
This problem follows the Shortest Path, Array, Matrix, Breadth-First Search, Graph, Depth-First Search, Heap (Priority Queue), String interview pattern. Dijkstra's algorithm is the most suitable approach:
dist matrix (and potentially a paths matrix) to store the best results found for each stopping point and only continue if a new path is better.Start (0,0), Hole (0,3), Wall at (0,4). Roll right ('r'):
In "The Maze III coding problem," a major pitfall is failing to check for the hole at every step of the roll. Many candidates only check the final stopping point, which misses cases where the ball rolls over the hole. Another mistake is incorrectly implementing the tie-breaker for lexicographical order, either by not including the path in the priority queue or by using an incorrect comparison logic.
When a problem asks for the "lexicographically smallest path," always consider how to integrate string comparisons into your shortest-path algorithm. In Python or Java, you can often include the string directly in your priority queue tuples. Practice writing "slide" logic that can be interrupted by a specific condition (like a hole) to prepare for these types of grid problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| The Maze II | Medium | Solve | |
| Minimum Obstacle Removal to Reach Corner | Hard | Solve | |
| Minimum Time to Visit a Cell In a Grid | Hard | Solve | |
| Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Solve | |
| Minimum Path Cost in a Hidden Grid | Medium | Solve |