← Meta Interview Insights

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

Senior
Jun 2026

Summary

Meta SWE coding round with four back-to-back algorithm problems. Solid mix of tree traversals and array work, nothing too exotic but the follow-ups on a couple of them added real pressure.

Questions Asked (4)

Q1

Given a binary tree, return lists of node values grouped by vertical column, from leftmost to rightmost. Nodes sharing the same column and row should appear in left-to-right order. What are the time and space complexities? Follow-up: how would you support streaming insertions and real-time queries?

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

The base problem I handled fine, BFS with a column offset tracked per node, dump into a sorted map.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then explain a BFS/DFS solution using a column index, ensuring correct ordering by row and left-to-right. Analyze time and space complexity, then discuss the follow-up by proposing a balanced BST or segment tree with column indices for dynamic insertions and queries.

Pro tip: Mention that using a hash map with min/max column tracking avoids sorting, and for the follow-up, highlight the trade-off between update and query times, suggesting a balanced BST keyed by column for O(log n) operations.

1. Clarify and Restate

Confirm the problem details: vertical order traversal, ordering within same column and row, and output format. Ask about tree size, balance, and whether node values are unique.

2. Outline Approach

Propose a BFS or DFS traversal that assigns a column index to each node (root at 0, left child -1, right child +1). Use a map from column to list of (row, value) and sort by row, then value for left-to-right order.

3. Analyze Complexity

State time complexity: O(n log n) due to sorting within columns, or O(n) if using BFS with level-order and tracking min/max columns. Space complexity: O(n) for storing nodes and the map.

4. Address Follow-up

For streaming insertions and real-time queries, suggest a balanced BST (e.g., AVL or Red-Black) keyed by column, where each node stores a list of values sorted by row. Insertions are O(log n) and queries O(log n + k) for k results.

5. Discuss Trade-offs

Compare the static solution (simple, O(n log n)) with the dynamic solution (more complex, but supports updates). Mention alternative data structures like segment trees or skip lists, and their pros/cons.

Key Points to Mention

  • Column index assignment: root at 0, left child -1, right child +1.
  • Use of BFS to ensure top-to-bottom and left-to-right ordering within same column and row.
  • Time complexity: O(n log n) with sorting, or O(n) with BFS and min/max column tracking.
  • Space complexity: O(n) for storing nodes and the map.
  • For streaming: balanced BST keyed by column, storing values sorted by row.
  • Trade-offs: static solution simpler but no updates; dynamic solution supports O(log n) insertions and queries.

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

Q2

Given a binary tree, return the sequence of nodes visible when looking at the tree from the right side. How do you handle tie-breaking and missing children? Provide both a BFS and a DFS solution with complexities.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Asked for both BFS and DFS which I liked, felt like they actually wanted to see if you understood the tradeoffs rather than just memorizing one approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem definition first: the right-side view consists of the rightmost node at each depth, with missing children simply skipped. Then present both BFS (level-order traversal, taking the last node per level) and DFS (pre-order traversal prioritizing right subtree, recording the first node seen at each depth) solutions, and compare their time/space complexities.

Pro tip: Mention that BFS naturally handles tie-breaking by processing levels left-to-right and taking the last node, while DFS requires a depth check to ensure only the first node encountered at each depth is recorded. Also note that both approaches are O(n) time, but BFS may use O(w) space (w = max width) while DFS uses O(h) space (h = height), which matters for skewed trees.

1. Clarify the problem

Define what 'right side view' means: the set of nodes visible when the tree is viewed from the right, which are the rightmost nodes at each depth. Confirm that missing children are simply absent and do not affect visibility.

2. Explain BFS approach

Use a queue to perform level-order traversal. For each level, process all nodes and record the value of the last node (the rightmost one). Enqueue left and right children as usual, skipping nulls.

3. Explain DFS approach

Use pre-order traversal, visiting the right child before the left. Keep track of the current depth and a result list; if the depth equals the result size, append the node's value (first visit at this depth).

4. Discuss tie-breaking and missing children

For BFS, tie-breaking is inherent: the last node in each level is the rightmost. For DFS, tie-breaking is handled by visiting right first, so the first node seen at a depth is the rightmost. Missing children are simply not enqueued or visited.

5. Compare complexities and trade-offs

Both solutions run in O(n) time. BFS uses O(w) space where w is the maximum width of the tree, while DFS uses O(h) space where h is the height. Choose based on tree shape: BFS may be memory-heavy for wide trees, DFS for deep trees.

Key Points to Mention

  • Definition of right-side view: rightmost node at each depth.
  • BFS: level-order traversal, take last node per level.
  • DFS: pre-order traversal (right before left), record first node per depth.
  • Tie-breaking: BFS naturally picks the last node; DFS relies on right-first order.
  • Missing children: simply skip null nodes; they do not affect visibility.
  • Complexities: O(n) time for both; BFS O(w) space, DFS O(h) space.

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

Q3

Given a sorted integer array and a target T, count the number of unique index pairs (i, j) where i < j and nums[i] + nums[j] equals T. Solve it in O(n) time with O(1) extra space, and handle duplicate values correctly.

Algorithms & Data Structures
Author's notes

Two pointers, pretty standard.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique starting from both ends of the sorted array. Move pointers inward based on the sum compared to the target, and when a match is found, count all duplicates of each value to account for multiple pairs efficiently. This achieves O(n) time and O(1) space.

Pro tip: Clarify that the array is sorted and that indices are unique even if values are duplicated. Emphasize that counting duplicates correctly is key to handling edge cases without extra space.

1. Initialize pointers and result

Set left pointer to 0, right pointer to n-1, and a counter for the number of pairs.

2. Iterate while left < right

Compute sum = nums[left] + nums[right]. If sum < T, increment left; if sum > T, decrement right.

3. Handle match with duplicates

When sum == T, count how many times nums[left] and nums[right] appear. If the values are different, add count_left * count_right to the result and move both pointers past their duplicates. If the values are the same, add count_left * (count_left - 1) / 2 and break.

4. Return result

After the loop, return the total count of unique index pairs.

Key Points to Mention

  • Two-pointer technique exploits sorted order to achieve O(n) time.
  • O(1) extra space by using only a few variables.
  • Duplicate handling: count consecutive equal elements and compute combinations.
  • Edge cases: empty array, single element, all elements same, no valid pairs.
  • Time complexity analysis: each element visited at most once.
  • Space complexity: no additional data structures used.

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

Q4

Given a binary tree, output a level-order traversal where each level alternates direction, left-to-right then right-to-left and so on. Discuss the tradeoffs between using a deque-based BFS approach versus a DFS approach that tracks the level number.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Classic zigzag.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then present a BFS solution using a deque to alternate direction per level. Follow with a DFS solution that tracks level and inserts nodes at the correct position based on level parity, and compare their tradeoffs in terms of time, space, and code complexity.

Pro tip: Mention that BFS is more intuitive for level-order traversal and avoids recursion depth issues, but DFS can be more memory-efficient for skewed trees; showing awareness of these practical considerations demonstrates maturity.

1. Clarify the problem

Restate the problem to ensure understanding: level-order traversal with alternating directions per level. Discuss edge cases like empty tree, single node, and skewed trees.

2. Present BFS with deque

Explain the BFS approach: use a queue to process nodes level by level, and a deque to collect nodes in the current level. Alternate between appending left-to-right and right-to-left based on level parity.

3. Present DFS with level tracking

Explain the DFS approach: recursively traverse the tree, passing the level number. For each node, insert its value into the result list at the appropriate level, either at the end or beginning based on level parity.

4. Compare tradeoffs

Discuss time and space complexity: both are O(n) time, but BFS uses O(w) space where w is max width, while DFS uses O(h) space for recursion stack. Also compare code readability and potential stack overflow in DFS for deep trees.

5. Conclude with recommendation

Summarize which approach is preferable in different scenarios, e.g., BFS for balanced trees or when level order is natural, DFS for memory-constrained environments with deep trees.

Key Points to Mention

  • Time complexity: both approaches are O(n) as each node is visited once.
  • Space complexity: BFS uses O(w) where w is maximum width; DFS uses O(h) for recursion stack, where h is height.
  • Deque operations: using a deque allows O(1) insertion at both ends, enabling efficient alternating order.
  • DFS level tracking: need to ensure result list has enough sublists for each level, and insert at correct position based on level parity.
  • Edge cases: empty tree, single node, skewed tree (DFS may cause stack overflow for very deep trees).
  • Code simplicity: BFS is often more straightforward for level-order traversal, but DFS can be more concise with recursion.

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