← Meta Interview Insights

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

Senior
May 2026

Summary

Meta SWE interview with a pretty gnarly tree traversal problem that had a few layers to it. The follow-up about zigzag order is what really separated people, I think.

Questions Asked (2)

Q1

Given a binary tree and a predicate function P(node), do a level-order traversal that collects only the nodes satisfying P at each level, using only stacks (no queues). Return a list of levels with nodes in left-to-right order. Walk through the algorithm, argue its correctness, and give time and space complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The stack-only constraint is what tripped me up first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two stacks to simulate level-order traversal: one stack for the current level and another for the next level. Process nodes from the current stack, and for each node, push its right child then left child onto the next stack to ensure left-to-right order when popped. Collect nodes that satisfy P at each level and return the list of levels.

Pro tip: Emphasize that using two stacks is a common pattern for level-order traversal without queues, and clarify that the order of pushing children (right then left) is crucial to maintain left-to-right processing. Also, mention that this approach can be extended to handle multiple levels by swapping stacks after each level.

1. Clarify the problem and constraints

Restate the problem to ensure understanding: level-order traversal using only stacks, collecting nodes that satisfy predicate P at each level, and returning a list of levels with nodes in left-to-right order. Confirm that the tree is binary and that P is a function that takes a node and returns a boolean.

2. Design the two-stack approach

Explain that you will use two stacks: currentLevel and nextLevel. Initialize currentLevel with the root. While currentLevel is not empty, pop nodes from currentLevel, and if they satisfy P, add them to the current level's result list. For each popped node, push its right child then left child onto nextLevel (to ensure left-to-right order when popped). After processing all nodes in currentLevel, swap currentLevel and nextLevel, and add the current level's result to the overall list.

3. Walk through an example

Trace the algorithm on a small binary tree (e.g., root with left and right children, and some grandchildren) to demonstrate how nodes are processed level by level and how the order is maintained. Show how the stacks evolve and how the predicate filters nodes.

4. Argue correctness

Prove that the algorithm processes nodes level by level in left-to-right order. Use induction on levels: assume that when processing level k, the currentLevel stack contains all nodes of level k in left-to-right order (top of stack is leftmost). Then, by pushing right then left children, the nextLevel stack will contain nodes of level k+1 in left-to-right order (top is leftmost). Thus, when swapped, the next iteration processes level k+1 correctly.

5. Analyze time and space complexity

Time complexity: O(N), where N is the number of nodes, since each node is pushed and popped exactly once. Space complexity: O(W), where W is the maximum width of the tree, as the stacks hold at most the number of nodes in two consecutive levels. In the worst case (e.g., perfect binary tree), W = O(N), so space is O(N).

Key Points to Mention

  • Two stacks are used to simulate level-order traversal without a queue.
  • Push right child before left child onto the next stack to ensure left-to-right order when popped.
  • The predicate P is applied when popping nodes from the current stack, and only satisfying nodes are added to the level result.
  • After processing a level, swap the current and next stacks and add the collected nodes to the result list.
  • Time complexity is O(N) because each node is visited once.
  • Space complexity is O(W) where W is the maximum width of the tree, which can be O(N) in the worst case.

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

Q2

Adapt the same stack-based level traversal to output nodes in zigzag order (alternating left-to-right and right-to-left by level), still without using any queues.

Algorithms & Data Structures
Author's notes

Actually liked this follow-up more than the original.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two stacks to simulate level-order traversal without queues: one stack for the current level and another for the next level. Alternate the order in which children are pushed (left-to-right or right-to-left) based on the current level's direction to achieve zigzag output.

Pro tip: Clarify that this approach uses O(n) space in the worst case (for a full level), which is optimal for level-order traversal, and mention that it avoids the overhead of queue operations while elegantly handling the zigzag pattern.

1. Initialize stacks and direction flag

Create two stacks: currentLevel and nextLevel. Push the root onto currentLevel. Set a boolean leftToRight to true, indicating the direction for the current level.

2. Process current level

While currentLevel is not empty, pop nodes one by one. For each node, output its value. Depending on leftToRight, push its children onto nextLevel in the appropriate order (left then right if leftToRight is true, else right then left).

3. Move to next level and toggle direction

After processing all nodes in currentLevel, swap currentLevel and nextLevel (i.e., currentLevel = nextLevel, nextLevel = empty stack). Toggle leftToRight to reverse the direction for the next level.

4. Repeat until all levels processed

Continue the loop until both stacks are empty, ensuring all levels are traversed and output in zigzag order.

Key Points to Mention

  • Use of two stacks to avoid queues and achieve level-order traversal.
  • Alternating push order of children based on level direction to produce zigzag output.
  • Time complexity: O(n) where n is the number of nodes, as each node is visited once.
  • Space complexity: O(w) where w is the maximum width of the tree, which is optimal for level-order traversal.
  • Handling edge cases: empty tree, single node, and skewed trees.
  • Comparison with queue-based BFS and why stacks are used here (constraint: no queues).

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