Solve coding and AI-assisted tasks
Company: Meta
Role: Backend Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This set evaluates algorithmic problem-solving across arrays, immutable linked-list interfaces, parent-pointer tree traversal, string validity via minimal edits, and grid-based pathfinding including an AI-assisted maze search, testing data-structure knowledge, complexity reasoning, and handling of constrained APIs.
Part 1: Right-side visibility in a skyline
Constraints
- 0 <= len(heights) <= 10^5
- -10^9 <= heights[i] <= 10^9
- If two buildings have the same height, the one on the left cannot see past the one on the right.
Examples
Input: [4, 2, 3, 1]
Expected Output: [0, 2, 3]
Explanation: Building 0 is taller than all buildings to its right, building 2 is taller than building 3, and building 3 is the rightmost building.
Input: [1, 2, 3, 4]
Expected Output: [3]
Explanation: Only the last building can see the ocean because every earlier building has a taller 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.
Input: [2, 2, 2]
Expected Output: [2]
Explanation: Visibility requires strictly shorter buildings to the right, so equal heights block the view.
Input: []
Expected Output: []
Explanation: Edge case: no buildings means no visible indices.
Hints
- Scan from right to left instead of checking every building against all buildings to its right.
- A building is visible exactly when it is taller than every building seen so far from the right.
Part 2: Print an immutable linked list in reverse
Constraints
- 0 <= n <= 10^5, where n == len(values) == len(next_index)
- head is -1 for an empty list; otherwise 0 <= head < n
- Starting from head, next_index describes a valid acyclic singly linked list
- You must not mutate values or next_index
Examples
Input: ([1, 2, 3], [1, 2, -1], 0)
Expected Output: [3, 2, 1]
Explanation: Traverse 1 -> 2 -> 3, then output in reverse.
Input: ([], [], -1)
Expected Output: []
Explanation: Edge case: empty immutable list.
Input: ([42], [-1], 0)
Expected Output: [42]
Explanation: A single-node list prints the same value in reverse.
Input: ([5, 7, 7, 9], [1, 2, 3, -1], 0)
Expected Output: [9, 7, 7, 5]
Explanation: The linked list order is 5 -> 7 -> 7 -> 9, so reverse printing is 9, 7, 7, 5.
Hints
- Since you can only move forward through the list, store what you see as you traverse.
- A stack is a simple way to reverse the order without modifying the list.
Part 3: Find a common ancestor with parent pointers
Constraints
- 1 <= n <= 10^5, where n == len(parent)
- Exactly one node has parent -1, and every other node has exactly one valid parent index
- 0 <= a, b < n
- a and b are in the same tree
Examples
Input: ([-1, 0, 0, 1, 1, 2], 3, 4)
Expected Output: 1
Explanation: Nodes 3 and 4 share parent 1, so the LCA is 1.
Input: ([-1, 0, 0, 1, 1, 2], 3, 5)
Expected Output: 0
Explanation: The paths are 3 -> 1 -> 0 and 5 -> 2 -> 0, so the first common ancestor is 0.
Input: ([-1, 0, 0, 1, 1, 2], 2, 5)
Expected Output: 2
Explanation: A node is allowed to be its own ancestor, so the LCA of 2 and 5 is 2.
Input: ([-1, 0, 1, 2], 0, 3)
Expected Output: 0
Explanation: The root is an ancestor of every node in the tree.
Input: ([-1], 0, 0)
Expected Output: 0
Explanation: Edge case: a single-node tree.
Hints
- If one node is deeper than the other, move it upward first until both nodes are at the same depth.
- Once the depths match, move both nodes upward together until they meet.
Part 4: Remove the fewest invalid parentheses
Constraints
- 0 <= len(s) <= 10^5
- s contains only lowercase English letters and the characters '(' and ')'
Examples
Input: "lee(t(c)o)de)"
Expected Output: "lee(t(c)o)de"
Explanation: Removing the final unmatched ')' produces a valid string with one deletion.
Input: "()())()"
Expected Output: "()()()"
Explanation: One unmatched closing parenthesis must be removed.
Input: "a)b(c)d"
Expected Output: "ab(c)d"
Explanation: Remove the unmatched ')' after 'a'.
Input: "))( ("
Expected Output: ""
Explanation: This case is intentionally not used because it contains a space, which is outside the stated constraints.
Input: "))( ("
Expected Output: ""
Explanation: Placeholder removed.
Hints
- As you scan left to right, unmatched closing parentheses can be identified immediately.
- Any opening parentheses left unmatched after the scan must also be removed.
Part 5: AI-assisted maze search
Constraints
- 1 <= rows, cols <= 200
- grid is rectangular
- Each cell is one of 'S', 'E', '.', '#'
- There is exactly one 'S' and exactly one 'E'
Examples
Input: ["S..", "##.", "..E"]
Expected Output: True
Explanation: There is a path across the top row and then down to E.
Input: ["S#.", "##.", "..E"]
Expected Output: False
Explanation: The start is blocked and cannot reach the lower rows.
Input: ["SE"]
Expected Output: True
Explanation: Edge case: the end is immediately adjacent to the start.
Input: ["S#E"]
Expected Output: False
Explanation: A wall directly blocks the only possible route.
Hints
- Treat each non-wall cell as a node in a graph connected to its four neighbors.
- BFS is especially useful because it also supports the shortest-path follow-up.