Navigate unknown grid to locate target
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and spatial reasoning skills, particularly graph traversal, exploration in unknown environments, state encoding for relative coordinates, and reasoning about correctness.
Constraints
- 0 <= number of rows, number of columns
- Each cell value is one of 0 (open), 1 (wall), 2 (cheese)
- At most one cell contains cheese in a valid maze
- start_row / start_col may be out of bounds or on a wall, in which case return False
- The mouse may only move to orthogonally adjacent (up/down/left/right) non-wall cells
Examples
Input: ([[0,0,2],[1,1,0],[0,0,0]], 0, 0)
Expected Output: True
Explanation: From (0,0) the mouse moves right to (0,1) then (0,2), which holds the cheese.
Input: ([[0,1,2],[0,1,0],[0,1,0]], 0, 0)
Expected Output: False
Explanation: A full column of walls separates the start's region from the cheese, so it is unreachable.
Input: ([[2]], 0, 0)
Expected Output: True
Explanation: The single starting cell already contains the cheese.
Input: ([[0]], 0, 0)
Expected Output: False
Explanation: A 1x1 grid with no cheese has nothing to find.
Input: ([[1,2],[0,0]], 0, 0)
Expected Output: False
Explanation: The start (0,0) is itself a wall, an invalid position, so the mouse cannot begin and returns False.
Input: ([[0,0,0],[0,1,0],[0,0,2]], 1, 0)
Expected Output: True
Explanation: Starting mid-grid, the mouse routes around the central wall to reach the cheese at (2,2).
Input: ([], 0, 0)
Expected Output: False
Explanation: An empty grid has no cells to explore.
Input: ([[0,0],[0,2]], 5, 5)
Expected Output: False
Explanation: The start position is out of bounds, so the search returns False immediately.
Hints
- The interactive move()/turnLeft()/turnRight()/atCheese() interface reduces to a graph traversal: every reachable open cell is a node, and the visited marker prevents infinite loops.
- A flood fill (DFS or BFS) from the start that marks each visited cell guarantees you explore the entire connected open region exactly once.
- Treat the cheese cell as passable (you can stand on it) and check for it as you visit each cell; remember to guard against an out-of-bounds or wall start position before traversing.