← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Jun 2026

Summary

Meta SWE interview that went deep on tree manipulation, specifically converting a BST to a doubly linked list in-place. They wanted both recursive and iterative approaches plus complexity analysis, and the follow-ups were where things got spicy.

Questions Asked (3)

Q1

Convert a binary search tree into a sorted doubly linked list in-place, reusing tree nodes so that left becomes prev and right becomes next. Return the head of the list. Also explain how you handle duplicate keys.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with the recursive in-order approach first, threading a 'prev' pointer through the traversal.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use an in-order traversal to visit nodes in sorted order, relinking each node's left pointer to the previous node and right pointer to the next. Maintain a prev pointer and a head pointer, updating them as you traverse. For duplicates, decide on a consistent policy (e.g., keep them in the list) and explain how the BST property handles them (e.g., duplicates go to the right subtree).

Pro tip: Clarify upfront whether duplicates are allowed and how they are stored in the BST; this shows you think about edge cases and data integrity. Also, mention that the conversion is in-place with O(n) time and O(h) space for recursion, and discuss iterative Morris traversal for O(1) space if asked.

1. Clarify requirements and constraints

Ask if duplicates are allowed, how they are handled in the BST (e.g., right subtree), and confirm in-place conversion with no new nodes. Also confirm return type (head of list).

2. Choose traversal and pointers

Use in-order traversal (recursive or iterative) to process nodes in sorted order. Maintain a 'prev' pointer to the last processed node and a 'head' pointer for the first node.

3. Relink nodes during traversal

For each node, set node.left = prev (if prev exists, set prev.right = node). Update prev to current node. At the end, set prev.right = null to terminate the list.

4. Handle duplicates explicitly

Explain that duplicates are included in the list in sorted order. If the BST stores duplicates in the right subtree, in-order traversal will visit them after the original, preserving stability.

5. Analyze complexity and edge cases

State time O(n) and space O(h) for recursion (or O(1) with Morris). Discuss edge cases: empty tree, single node, skewed tree, and duplicates.

Key Points to Mention

  • In-order traversal yields sorted order for BST.
  • In-place conversion: reuse left as prev and right as next.
  • Maintain prev and head pointers to build the list.
  • Duplicates: clarify policy; typically keep them and they appear consecutively in sorted order.
  • Time complexity O(n), space O(h) for recursion; mention iterative Morris for O(1) space.
  • Edge cases: empty tree, single node, skewed tree, and duplicate keys.

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

Q2

Modify your solution to produce a circular doubly linked list instead of a linear one.

Algorithms & Data Structures
Author's notes

Not too bad once the base solution is done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the original problem and the existing linear doubly linked list implementation. Then, explain how to adjust the pointers so that the last node's next points to the head and the head's prev points to the last node, ensuring all operations maintain circularity. Finally, discuss edge cases and how to test the modified structure.

Pro tip: Emphasize that circularity simplifies edge cases like insertion at the beginning or end, but requires careful handling to avoid infinite loops during traversal. Mention that you would add a sentinel node or a size counter to make operations cleaner.

1. Clarify the original problem

Restate the original problem and the existing linear doubly linked list solution to ensure alignment with the interviewer.

2. Identify changes for circularity

Determine which pointers need modification: the last node's next should point to the head, and the head's prev should point to the last node.

3. Adjust operations

Modify insertion, deletion, and traversal methods to maintain the circular structure, handling cases like empty list and single node.

4. Handle edge cases

Discuss how to avoid infinite loops during traversal and ensure operations work correctly when the list is empty or has one node.

5. Test and validate

Outline a testing strategy: unit tests for each operation, checking forward and backward traversal, and verifying circular links.

Key Points to Mention

  • Pointer adjustments: last.next = head and head.prev = last
  • Edge cases: empty list, single node, insertion at head/tail
  • Traversal termination condition (e.g., stop when returning to head)
  • Use of sentinel node to simplify operations
  • Maintaining size counter for O(1) length retrieval
  • Time and space complexity remains O(1) for insert/delete at known positions

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

Q3

If recursion is not allowed, how do you achieve O(1) auxiliary space beyond the output pointers? Walk through Morris traversal as a solution.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got stuck.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that O(1) auxiliary space means no recursion stack or explicit stack, and that Morris traversal achieves this by temporarily modifying the tree using threaded links. Then walk through the algorithm step-by-step, emphasizing how it restores the tree and handles edge cases.

Pro tip: Mention that while Morris traversal is space-efficient, it temporarily mutates the tree, which can be problematic in concurrent or read-only scenarios—showing awareness of trade-offs beyond just complexity.

1. Clarify constraints and goal

Confirm that 'O(1) auxiliary space' excludes recursion and explicit stacks, and that the output list/array is not counted. State that Morris traversal achieves this by using the tree's null right pointers as temporary threads.

2. Explain the threading concept

Describe how for each node, you find its inorder predecessor and set the predecessor's right pointer to the current node, creating a temporary link. This allows returning to the current node after traversing the left subtree.

3. Walk through the algorithm

Outline the loop: while current is not null, if no left child, visit current and move right; else find predecessor, if predecessor's right is null, set it to current and move left; if it points to current, reset it to null, visit current, and move right.

4. Highlight restoration and edge cases

Emphasize that the temporary links are removed when revisiting the predecessor, restoring the tree. Mention handling of single-node trees, skewed trees, and the fact that each edge is traversed at most twice, giving O(n) time.

5. Discuss trade-offs and alternatives

Acknowledge that Morris traversal modifies the tree temporarily, which may not be suitable for concurrent access. Compare with recursive/stack-based approaches that use O(h) space but are simpler and non-mutating.

Key Points to Mention

  • O(1) auxiliary space means no recursion stack or explicit stack; output space is excluded.
  • Morris traversal uses temporary threaded links (right pointers of predecessors) to navigate without extra space.
  • The algorithm restores the tree by removing the temporary links when they are no longer needed.
  • Time complexity remains O(n) because each edge is traversed at most twice.
  • Trade-off: temporary mutation of the tree can be problematic in read-only or concurrent environments.
  • Morris traversal can be adapted for preorder and postorder, but inorder is most straightforward.

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