Solve Tree Columns And Maze Variants
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates tree traversal and column-based grouping skills along with grid-based graph search, state modeling, and handling dynamic maze behaviors such as directional constraints, impassable walls, and bomb-triggered grid updates.
Part 1: Binary Tree Column Grouping
Constraints
- 0 <= number of list entries <= 10^4
- Each non-None value is an integer
- The input list represents a valid level-order binary tree serialization
Examples
Input: [3, 9, 20, None, None, 15, 7]
Expected Output: [[9], [3, 15], [20], [7]]
Explanation: Node 9 is in column -1, nodes 3 and 15 are in column 0, node 20 is in column 1, and node 7 is in column 2.
Input: [1, 2, 3, 4, 6, 5, 7]
Expected Output: [[4], [2], [1, 6, 5], [3], [7]]
Explanation: Nodes 6 and 5 share the same row and column, so they appear in level-order left-to-right order: 6 before 5.
Input: []
Expected Output: []
Explanation: An empty tree has no columns.
Input: [1]
Expected Output: [[1]]
Explanation: A single-node tree has one column.
Hints
- A breadth-first traversal naturally preserves the required left-to-right order for nodes that land in the same row and column.
- Track each node together with its column index, then gather values by column.
Part 2: Maze Search - Handle Empty Grid Input Safely
Constraints
- 0 <= m, n <= 200
- All rows in `grid` have the same length
- Movement is allowed only up, down, left, and right
Examples
Input: (["...", "..."], (0, 0), (1, 2))
Expected Output: 3
Explanation: A shortest path is Right, Right, Down.
Input: ([], (0, 0), (0, 0))
Expected Output: -1
Explanation: The grid is empty, so no path search is possible.
Input: (["."], (0, 0), (0, 0))
Expected Output: 0
Explanation: Start and target are the same valid cell.
Input: (["..", ".."], (0, 0), (2, 0))
Expected Output: -1
Explanation: The target coordinates are outside the grid.
Hints
- Check `not grid` or `not grid[0]` before using `len(grid[0])`.
- Because every move costs 1, breadth-first search gives the shortest path.
Part 3: Maze Search - Add Wall Cells
Constraints
- 0 <= m, n <= 200
- All rows in `grid` have the same length
- Cells are either '.' or '#'
Examples
Input: (["...", ".#.", "..."], (0, 0), (2, 2))
Expected Output: 4
Explanation: The path goes around the center wall.
Input: ([".#.", "###", "..."], (0, 0), (2, 2))
Expected Output: -1
Explanation: The start region is completely cut off by walls.
Input: (["#"], (0, 0), (0, 0))
Expected Output: -1
Explanation: The start cell is a wall, so the state is invalid.
Input: (["."], (0, 0), (0, 0))
Expected Output: 0
Explanation: Start already equals target.
Hints
- This is a standard shortest-path-on-an-unweighted-grid problem, so BFS is a good fit.
- Treat walls as cells that simply never become neighbors.
Part 4: Maze Search - Add Forced Movement from Arrow Cells
Constraints
- 0 <= m, n <= 200
- All rows in `grid` have the same length
- Cells are '.', '^', 'v', '<', or '>'
Examples
Input: ([".>.", "..."], (0, 0), (1, 2))
Expected Output: 3
Explanation: One shortest path is Right to '>', forced Right, then Down.
Input: (["v.", "^."], (0, 0), (1, 1))
Expected Output: -1
Explanation: The arrow cells create a loop between (0,0) and (1,0), so the target is unreachable.
Input: (["^"], (0, 0), (0, 0))
Expected Output: 0
Explanation: Being already at the target requires zero moves, even if the cell is an arrow.
Input: ([], (0, 0), (0, 0))
Expected Output: -1
Explanation: An empty grid is invalid input for search.
Hints
- The allowed outgoing moves depend on the current cell, not on the destination cell.
- Even with forced moves, BFS still works because every move has equal cost.
Part 5: Maze Search - Combine Arrow Cells and Wall Cells
Constraints
- 0 <= m, n <= 200
- All rows in `grid` have the same length
- Cells are '.', '#', '^', 'v', '<', or '>'
Examples
Input: ([".>.", ".#.", "..."], (0, 0), (2, 2))
Expected Output: 4
Explanation: A shortest path is Right to '>', forced Right, then Down, Down.
Input: ([">#", ".."], (0, 0), (1, 1))
Expected Output: -1
Explanation: The start cell is an arrow that immediately points into a wall, so no move is possible.
Input: (["#"], (0, 0), (0, 0))
Expected Output: -1
Explanation: The start cell is blocked.
Input: (["v.", ".."], (0, 0), (0, 0))
Expected Output: 0
Explanation: Start already equals target.
Input: ([], (0, 0), (0, 0))
Expected Output: -1
Explanation: An empty grid should be handled safely.
Hints
- Generate neighbors based on the current cell: arrows give one possible move, while normal cells give four.
- If an arrow points out of bounds or into a wall, that state simply has no valid outgoing move in that direction.
Part 6: Maze Search - Add Bomb Cells That Clear Adjacent Walls
Constraints
- 0 <= m, n <= 30
- All rows in `grid` have the same length
- The number of bomb cells is at most 12
- Movement is only up, down, left, and right
Examples
Input: ([".B#", "##.", "..."], (0, 0), (1, 2))
Expected Output: 3
Explanation: Move to the bomb in 1 step, which clears adjacent walls, then continue through a newly passable route to the target.
Input: ([".>B", "#.#", "..."], (0, 0), (2, 2))
Expected Output: 4
Explanation: The arrow forces you into the bomb, and the bomb clears the wall below it so the target becomes reachable.
Input: (["B#.", "..."], (0, 0), (0, 2))
Expected Output: 2
Explanation: The start cell is a bomb, so the wall at (0,1) is cleared immediately.
Input: ([">#", "B."], (0, 0), (1, 1))
Expected Output: -1
Explanation: The start arrow points directly into a wall, so the bomb can never be reached.
Input: ([], (0, 0), (0, 0))
Expected Output: -1
Explanation: An empty grid is invalid input.
Hints
- Reaching the same cell with different bombs already triggered can lead to different future options, so position alone is not a sufficient visited state.
- A bitmask over triggered bombs is a compact way to represent which walls are currently cleared on that path.