Magicsheet logo

The Maze III

Hard
12.5%
Updated 8/1/2025

The Maze III

What is this problem about?

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.

Why is this asked in interviews?

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.

Algorithmic pattern used

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:

  1. Each state in the priority queue is a tuple: (distance, path_string, x, y).
  2. The priority queue sorts by distance first, then by the path string.
  3. For each direction, you roll the ball. During the roll, you check at every step if the current cell is the hole.
  4. If the ball hits the hole, the roll ends there. Otherwise, it ends at the cell before a wall.
  5. You maintain a 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.

Example explanation

Start (0,0), Hole (0,3), Wall at (0,4). Roll right ('r'):

  • Step 1: (0,1) - Not hole.
  • Step 2: (0,2) - Not hole.
  • Step 3: (0,3) - IS HOLE! Ball stops. Distance = 3, Path = "r". Roll down ('d') then right ('r') then up ('u'):
  • Suppose this also reaches the hole at (0,3) with distance 3.
  • Path would be "dru". Comparing "r" and "dru" lexicographically: "d" < "r", so "dru" would actually be checked earlier by the priority queue if the distances were equal. Wait, "d" comes before "r" in the alphabet. So "dru" is smaller than "r".

Common mistakes candidates make

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.

Interview preparation tip

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.

Similar Questions