Extend BFS maze solver with keys and arrows
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates mastery of breadth-first search, state-space modeling for grid-based traversal, and handling augmented states like keys and one-way tiles, testing competency in algorithm design and correctness within the Coding & Algorithms domain (graph algorithms).
Part 1: Render a Maze Path Exactly
Constraints
- 1 <= rows, cols <= 200
- grid is rectangular
- grid contains exactly one 'S' and one 'E'
- path may be empty
- Repeated coordinates may appear in path
Examples
Input: (["S..", ".#E"], [(0, 0), (0, 1), (0, 2), (1, 2)])
Expected Output: "S**\n.#E"
Explanation: The path overlays two '.' cells with '*', while 'S' and 'E' stay unchanged.
Input: (["SE"], [(0, 0), (0, 1)])
Expected Output: "SE"
Explanation: A path containing only start and end does not change the maze.
Input: (["S#E", "..."], [])
Expected Output: "S#E\n..."
Explanation: Empty path means the original maze is returned exactly.
Input: (["S.E"], [(0, 0), (0, 1), (0, 2), (0, 1)])
Expected Output: "S*E"
Explanation: Repeated coordinates are harmless; the middle cell is still rendered once as '*'.
Hints
- Convert each row to a mutable list before changing characters.
- Only replace cells that are not 'S', 'E', or '#'.
Part 2: Shortest Path in a Maze with Correct BFS Visited Handling
Constraints
- 1 <= rows, cols <= 200
- grid is rectangular
- grid contains exactly one 'S' and one 'E'
- Only '#', '.', 'S', and 'E' appear
Examples
Input: ["SE"]
Expected Output: 1
Explanation: The exit is adjacent to the start.
Input: ["S..", ".#.", "..E"]
Expected Output: 4
Explanation: One shortest path is right, right, down, down.
Input: ["S#E"]
Expected Output: -1
Explanation: The wall blocks the only possible route.
Input: ["S...", ".##.", "...E"]
Expected Output: 5
Explanation: The maze contains loops, but BFS with correct visited handling still finds the shortest route.
Hints
- In an unweighted grid, BFS is the standard way to find the shortest path.
- Mark a cell as visited when you enqueue it, not after you dequeue it.
Part 3: Shortest Path with One-Way Arrow Tiles
Constraints
- 1 <= rows, cols <= 200
- grid is rectangular
- grid contains exactly one 'S' and one 'E'
- Tiles may be '#', '.', 'S', 'E', '<', '>'
Examples
Input: ["S>E"]
Expected Output: 2
Explanation: From '>' you are forced to continue right into the exit.
Input: ["E<S"]
Expected Output: 2
Explanation: From '<' you are forced left, which reaches the exit.
Input: ["S<E"]
Expected Output: -1
Explanation: After stepping onto '<', you may only go back left, so the exit on the right is unreachable.
Input: ["S#..", ".>#E", "...."]
Expected Output: 6
Explanation: The arrow path is a trap, so the shortest route goes around the bottom row.
Input: ["S.E"]
Expected Output: 2
Explanation: A maze without arrows should still behave like normal BFS.
Hints
- The allowed outgoing directions depend on the current cell, not the destination cell.
- You can keep BFS unchanged except for the neighbor-generation logic.
Part 4: Shortest Path with Keys and Locks
Constraints
- 1 <= rows, cols <= 200
- grid is rectangular
- grid contains exactly one 'S' and one 'E'
- At most 6 distinct keys appear
- The exit tile 'E' is not treated as a lock
Examples
Input: ["SE"]
Expected Output: 1
Explanation: The exit is adjacent to the start.
Input: ["SaAE"]
Expected Output: 3
Explanation: Pick up 'a', then pass through lock 'A' to reach the exit.
Input: ["S.AE", ".##.", "a#.."]
Expected Output: 7
Explanation: You must go down to collect the key, then revisit earlier cells to pass through 'A'.
Input: ["SAE"]
Expected Output: -1
Explanation: The lock 'A' blocks the path, and no 'a' key exists.
Hints
- A 6-bit bitmask is enough to represent all collected keys.
- Visited should include both position and key mask; the same cell may need to be explored again after picking up a key.