Solve array, tree, and maze problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates algorithmic problem-solving across arrays, binary trees, and grid pathfinding, testing competencies in array traversal and visibility reasoning, vertical column grouping and ordering in trees, and reachability/search in 2D grids.
Part 1: Building Visibility from the Right
Constraints
- 0 <= len(heights) <= 100000
- 1 <= heights[i] <= 1000000000
Examples
Input: [4, 2, 3, 1]
Expected Output: [0, 2, 3]
Explanation: Building 3 sees the ocean. Building 2 is taller than building 3, so it also sees the ocean. Building 1 is blocked by building 2. Building 0 is taller than every building to its right.
Input: [4, 3, 2, 1]
Expected Output: [0, 1, 2, 3]
Explanation: Each building is taller than all buildings to its right, so every building can see the ocean.
Input: []
Expected Output: []
Explanation: There are no buildings, so the result is an empty list.
Input: [2, 2, 2]
Expected Output: [2]
Explanation: Only the last building can see the ocean. The others are blocked because buildings to their right are not strictly shorter.
Input: [7]
Expected Output: [0]
Explanation: A single building always sees the ocean.
Input: [2, 1, 2, 1]
Expected Output: [2, 3]
Explanation: Building 3 sees the ocean. Building 2 is taller than building 3, so it also sees. Building 1 is blocked by building 2, and building 0 is blocked by building 2 because equal height does not count as strictly shorter.
Hints
- Try scanning from right to left instead of checking every building's entire suffix.
- Keep track of the tallest building seen so far from the right.
Part 2: Binary Tree Vertical Grouping
Constraints
- 0 <= len(values) <= 10000
- Each non-None value is an integer in the range [-10000, 10000]
- The input list is a valid level-order encoding of a binary tree
Examples
Input: ([3, 9, 20, None, None, 15, 7],)
Expected Output: [[9], [3, 15], [20], [7]]
Explanation: Node 9 is in the left column, nodes 3 and 15 share the middle-left column from top to bottom, node 20 is next, and node 7 is the rightmost.
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 are reported in breadth-first order: 6 before 5.
Input: ([],)
Expected Output: []
Explanation: An empty tree has no vertical columns.
Input: ([1],)
Expected Output: [[1]]
Explanation: A single-node tree has exactly one column.
Input: ([1, 2, 3, None, 4, 5, None],)
Expected Output: [[2], [1, 4, 5], [3]]
Explanation: Nodes 4 and 5 both land in the root's column and are listed in breadth-first visitation order.
Hints
- Assign a column index to each node: root = 0, left child = column - 1, right child = column + 1.
- A breadth-first traversal naturally preserves the required order for nodes that land in the same row and column.
Part 3: Maze Pathfinding
Constraints
- 0 <= number of rows, number of columns <= 200
- Each cell in `grid` is either 0 or 1
- Movement is allowed only in the four cardinal directions
Examples
Input: ([ [0, 0, 1], [1, 0, 1], [1, 0, 0] ], (0, 0), (2, 2))
Expected Output: True
Explanation: One valid path is (0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2).
Input: ([ [0, 1, 0], [1, 1, 0], [0, 0, 0] ], (0, 0), (2, 2))
Expected Output: False
Explanation: The start cell is boxed in, so no path exists.
Input: ([[0]], (0, 0), (0, 0))
Expected Output: True
Explanation: The start and target are the same open cell.
Input: ([], (0, 0), (0, 0))
Expected Output: False
Explanation: An empty grid cannot contain a valid path.
Input: ([ [0, 1], [0, 0] ], (0, 1), (1, 1))
Expected Output: False
Explanation: The start position is a wall, so the path is invalid immediately.
Hints
- Model the open cells as a graph and run a traversal from the start cell.
- Keep a visited set so you do not revisit cells and loop forever.