← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Coding round at Meta with five pretty meaty algorithm problems back to back. The questions ranged from matrix traversal to tree stuff to a maze problem with four sub-parts, so there was a lot of ground to cover. Not sure how I did on the harder maze variants.

Questions Asked (5)

Q1

Given an m x n integer matrix, return all elements in diagonal order where cells sharing the same row+column sum are grouped together, and the traversal direction alternates between diagonals (up-right then down-left). Handle the empty matrix case.

Algorithms & Data Structures
Author's notes

I knew the diagonal grouping trick (row+col = constant) but fumbled the direction alternation logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Group cells by their row+column sum, which identifies each diagonal. Then iterate through the diagonals in order, alternating the direction of traversal (up-right or down-left) based on the diagonal index. Handle the empty matrix case by returning an empty array immediately.

Pro tip: Clarify the expected output format and edge cases (e.g., empty matrix, single row/column) before coding, and discuss time/space complexity upfront to demonstrate thoroughness.

1. Understand the problem and edge cases

Confirm that diagonals are defined by equal row+column sums, and that traversal alternates direction starting with up-right. Check for empty matrix and single row/column cases.

2. Choose a data structure to group diagonals

Use a hash map (dictionary) where keys are diagonal indices (row+col) and values are lists of elements in that diagonal. Alternatively, compute the range of diagonal indices and iterate directly.

3. Populate the diagonals

Traverse the matrix once, appending each element to the list corresponding to its diagonal index. This takes O(m*n) time.

4. Output diagonals with alternating direction

Iterate through diagonal indices from 0 to m+n-2. For even indices, output the diagonal in reverse order (down-left); for odd indices, output in normal order (up-right).

5. Analyze complexity and test

State that time and space complexity are O(m*n). Walk through a small example to verify correctness, including edge cases.

Key Points to Mention

  • Diagonal index = row + column, ranging from 0 to m+n-2.
  • Alternating direction: even diagonals go down-left, odd diagonals go up-right (or vice versa depending on starting direction).
  • Using a hash map to group elements by diagonal index simplifies the logic.
  • Time complexity: O(m*n) to visit each cell once; space complexity: O(m*n) for the output.
  • Edge cases: empty matrix (return []), single row, single column.
  • Avoid simulating movement with boundary checks; direct grouping is cleaner and less error-prone.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

In a binary tree where each node holds a single digit 0-9, each root-to-leaf path forms a number by concatenating digits top to bottom. Return the total sum of all such numbers.

Algorithms & Data Structures
Author's notes

Pretty straightforward DFS.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a depth-first search (DFS) traversal, passing the current number formed so far (as an integer) down to each child. At each leaf, add the current number to a running total. This avoids string concatenation and handles large numbers efficiently.

Pro tip: Mention that you can also solve it iteratively with a stack to avoid recursion depth issues, and discuss how to handle potential integer overflow by using modulo or big integers if the tree is very deep.

1. Clarify the problem

Confirm that each root-to-leaf path forms a number, and that the sum includes all such numbers. Ask about edge cases like empty tree or single node.

2. Choose traversal method

Decide between recursive DFS or iterative stack-based DFS. Explain that both work, but recursion is simpler and more readable.

3. Define recursive helper

Write a helper function that takes a node and the current number. At each node, update the number as current * 10 + node.val. If leaf, add to sum; else recurse on children.

4. Handle base cases

If the tree is empty, return 0. If a node is a leaf, add its number to the total. Ensure null children are skipped.

5. Analyze complexity

Time complexity is O(N) where N is number of nodes, as each node is visited once. Space complexity is O(H) for recursion stack, where H is tree height.

Key Points to Mention

  • Depth-first search (DFS) traversal
  • Passing current number as an integer (current * 10 + node.val)
  • Leaf node detection (no left and no right child)
  • Time complexity O(N) and space complexity O(H)
  • Handling empty tree (return 0)
  • Potential integer overflow for deep trees and possible mitigations

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

You have a nested list where each element is either an integer or another nested list. Return the sum of all integers, each weighted by its nesting depth (outermost list = depth 1).

Algorithms & Data Structures
Author's notes

Classic recursive problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and constraints, then discuss a recursive DFS solution that traverses the nested list while tracking depth. Emphasize that each integer contributes value * depth to the total sum, and analyze time and space complexity.

Pro tip: Mention that you can avoid recursion depth issues by using an iterative stack-based approach, and discuss how to handle very deep nesting or large inputs. This shows awareness of production constraints beyond the basic solution.

1. Clarify and Confirm

Ask clarifying questions about input size, nesting depth limits, and whether the list can be empty. Confirm that depth starts at 1 for the outermost list.

2. Outline the Approach

Propose a recursive DFS that passes the current depth. For each element, if it's an integer, add value * depth; if it's a list, recurse with depth + 1.

3. Walk Through an Example

Trace a small example like [1, [2, [3]]] to demonstrate correctness: 1*1 + 2*2 + 3*3 = 14. This validates the logic and catches edge cases.

4. Analyze Complexity

State that time complexity is O(N) where N is the total number of elements (including nested lists), and space complexity is O(D) for recursion depth D.

5. Discuss Optimizations and Edge Cases

Mention iterative stack-based alternative to avoid recursion limits, and handle edge cases like empty list, single integer, or deeply nested structures.

Key Points to Mention

  • Recursive DFS with depth parameter
  • Weighted sum: integer * depth
  • Time complexity O(N), space O(D) for recursion
  • Iterative stack-based alternative for deep nesting
  • Edge cases: empty list, non-integer elements, negative numbers
  • Clarify depth definition (outermost = 1)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

Each node in a binary tree has left, right, and parent pointers. Given references to two nodes but not the root, find their lowest common ancestor.

Algorithms & Data Structures
Author's notes

No root reference is the interesting constraint here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the parent pointers to find the depth of each node, then align them to the same depth by moving the deeper node up. Finally, move both nodes up simultaneously until they meet; that meeting point is the lowest common ancestor.

Pro tip: Clarify with the interviewer whether the nodes are guaranteed to be in the same tree and whether the tree can be mutated. Also, mention that this approach is O(h) time and O(1) space, which is optimal given the constraints.

1. Clarify assumptions

Confirm that both nodes are in the same tree and that parent pointers are valid. Ask if the tree can be modified or if extra space is allowed.

2. Compute depths

Write a helper function to compute the depth of a node by traversing parent pointers up to the root. Compute depths for both given nodes.

3. Align depths

If depths differ, move the deeper node up by the difference in depths so that both nodes are at the same level.

4. Find LCA

Move both nodes up simultaneously until they point to the same node. That node is the lowest common ancestor.

5. Analyze complexity

State that the time complexity is O(h) where h is the height of the tree, and space complexity is O(1) since only a few pointers are used.

Key Points to Mention

  • Use parent pointers to traverse upwards without needing the root.
  • Compute depth by traversing to the root; depth difference is key to alignment.
  • Align nodes to the same depth before moving together.
  • The first common node encountered when moving up simultaneously is the LCA.
  • Time complexity O(h) and space complexity O(1).
  • Edge cases: one node is ancestor of the other, nodes are the same, or tree is skewed.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

A ball rolls in a grid (0 = open, 1 = wall) and only stops when it hits a wall or boundary. Implement four things: (a) can the ball reach the target stop cell, (b) minimum cells traveled to reach the target or -1, (c) if a hole exists that catches the ball when it passes over, return the lexicographically smallest direction string among all shortest paths, (d) how would you optimize for repeated queries on the same static maze.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

Part (a) was BFS over stop positions, fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each node is a stop cell (where the ball can rest after hitting a wall or boundary), and edges represent rolling in one of the four directions until stopping. Use BFS to find reachability and shortest path in terms of number of rolls, but track cells traveled separately. For part (c), modify BFS to consider holes as intermediate stopping points and track lexicographically smallest direction string among shortest paths. For part (d), discuss precomputing all-pairs shortest paths or using bidirectional BFS with memoization for repeated queries.

Pro tip: Clarify upfront whether 'cells traveled' counts each cell entered or just the number of rolls, and whether the ball can stop on the target if it's not against a wall. Also, for lexicographic order, define the direction priority (e.g., 'd' < 'l' < 'r' < 'u') and ensure your BFS explores in that order to naturally get the smallest string.

1. Clarify problem details and edge cases

Ask about grid dimensions, whether the ball starts at a given cell, if the target is a stop cell, and how holes affect movement. Confirm direction ordering for lexicographic comparison.

2. Model as graph and choose BFS

Define nodes as stop cells. Precompute for each open cell and direction the next stop cell and distance traveled. Use BFS to find reachability and minimum rolls, while tracking total cells traveled.

3. Handle holes and lexicographic path

For part (c), treat holes as possible stopping points if the ball passes over them. During BFS, when multiple paths have the same length, choose the one with lexicographically smallest direction string by exploring directions in sorted order.

4. Optimize for repeated queries

For static maze, precompute all-pairs shortest paths between all stop cells using BFS from each stop cell, or use bidirectional BFS with caching. Discuss trade-offs between precomputation time and query time.

5. Analyze complexity and trade-offs

State time and space complexity for each part. For repeated queries, compare precomputation O(V*(V+E)) vs per-query BFS O(V+E) and suggest when each is appropriate.

Key Points to Mention

  • Graph modeling: nodes as stop cells, edges as rolls in four directions.
  • BFS for shortest path in terms of number of rolls, but track cells traveled separately.
  • Precomputation of next stop cell and distance for each cell and direction to avoid redundant simulation.
  • Lexicographic order: explore directions in sorted order (e.g., 'd', 'l', 'r', 'u') to get smallest string.
  • Holes: treat as intermediate stopping points; BFS must consider paths that pass over holes.
  • Optimization for repeated queries: all-pairs shortest paths via BFS from each stop cell, or bidirectional BFS with memoization.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.