Solve tree traversal and two-pointer problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: Solve tree traversal and two-pointer problems evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Vertical Order Traversal of a Binary Tree
Constraints
- 0 <= number of nodes <= 10^4
- Node values fit in a 32-bit signed integer
- tree is a valid level-order encoding where every present node lists both child slots (None for missing)
Examples
Input: ([3,9,20,None,None,15,7],)
Expected Output: [[9], [3, 15], [20], [7]]
Explanation: Columns -1..2: [9],[3,15],[20],[7]. Node 3 (col 0,row 0) precedes 15 (col 0,row 2).
Input: ([1,2,3,4,5,6,7],)
Expected Output: [[4], [2], [1, 5, 6], [3], [7]]
Explanation: Column 0 holds root 1 (row0), then 5 and 6 (both row2); 5 appears before 6 in BFS so it comes first.
Input: ([],)
Expected Output: []
Explanation: Empty tree yields no columns.
Input: ([1],)
Expected Output: [[1]]
Explanation: Single node sits in column 0.
Input: ([1,2,3,4,6,5,7],)
Expected Output: [[4], [2], [1, 6, 5], [3], [7]]
Explanation: In column 0, node 6 (left child of 2) is visited before node 5 (right child of 3) in BFS, preserving left-to-right appearance.
Hints
- Track a (column, row) coordinate for every node: root is (0,0); a left child is (col-1, row+1), a right child is (col+1, row+1).
- Use BFS so that nodes are visited top-to-bottom and left-to-right, giving you a natural tie-break order for same (row, column) nodes.
- Bucket nodes by column, then emit columns in increasing column index; within a column sort by (row, appearance order).
Binary Tree Right Side View
Constraints
- 0 <= number of nodes <= 10^4
- Node values fit in a 32-bit signed integer
- tree is a valid level-order encoding (None for missing children)
Examples
Input: ([1,2,3,None,5,None,4],)
Expected Output: [1, 3, 4]
Explanation: Depth 0:1, depth 1 rightmost:3, depth 2 rightmost:4 (5 is hidden behind 4).
Input: ([1,None,3],)
Expected Output: [1, 3]
Explanation: Right-leaning: 1 then 3.
Input: ([],)
Expected Output: []
Explanation: Empty tree yields an empty view.
Input: ([1],)
Expected Output: [1]
Explanation: Only the root is visible.
Input: ([1,2,3,4],)
Expected Output: [1, 3, 4]
Explanation: Depth 2 has only node 4 (left child of 2), so it is the rightmost visible node at that depth.
Hints
- BFS level by level: the last node you dequeue on each level is the one visible from the right.
- DFS alternative: recurse right subtree before left, and record a node the first time you reach a new depth.
- Missing children contribute nothing — only push real (non-None) children into the queue.
Count Unique Two-Sum Pairs in a Sorted Array
Constraints
- 0 <= len(nums) <= 10^5
- nums is sorted in non-decreasing order
- -10^9 <= nums[i], target <= 10^9
Examples
Input: ([1, 2, 3, 4, 5], 6)
Expected Output: 2
Explanation: Distinct pairs summing to 6: (1,5) and (2,4).
Input: ([1, 1, 2, 2, 3, 3], 4)
Expected Output: 2
Explanation: (1,3) and (2,2); duplicates do not add extra pairs.
Input: ([2, 2, 2, 2], 4)
Expected Output: 1
Explanation: Only the value pair (2,2), counted once.
Input: ([], 5)
Expected Output: 0
Explanation: No elements, no pairs.
Input: ([5], 5)
Expected Output: 0
Explanation: A pair needs two distinct indices.
Input: ([-3, -1, 0, 1, 2, 3], 0)
Expected Output: 2
Explanation: (-3,3) and (-1,1); there is only one 0 so (0,0) is impossible.
Input: ([1, 2, 3], 100)
Expected Output: 0
Explanation: No pair reaches the target.
Hints
- Use two pointers from both ends; if the sum is too small move left in, if too large move right in.
- On a match, skip ALL duplicates of the left value and ALL duplicates of the right value so each distinct value pair is counted exactly once.
- When the two pointers land on equal values that sum to target (e.g. target/2), that is a single valid pair — count it once and stop.
Binary Tree Zigzag Level Order Traversal
Constraints
- 0 <= number of nodes <= 10^4
- Node values fit in a 32-bit signed integer
- tree is a valid level-order encoding (None for missing children)
Examples
Input: ([3,9,20,None,None,15,7],)
Expected Output: [[3], [20, 9], [15, 7]]
Explanation: Level 0 L-to-R [3]; level 1 R-to-L [20,9]; level 2 L-to-R [15,7].
Input: ([1,2,3,4,5,6,7],)
Expected Output: [[1], [3, 2], [4, 5, 6, 7]]
Explanation: Level 1 reversed to [3,2]; level 2 left-to-right.
Input: ([],)
Expected Output: []
Explanation: Empty tree yields no levels.
Input: ([1],)
Expected Output: [[1]]
Explanation: Single root level.
Input: ([1,2,3,None,4,None,5],)
Expected Output: [[1], [3, 2], [4, 5]]
Explanation: Level 1 reversed to [3,2]; level 2 is [4,5] left-to-right (4 from node 2's right, 5 from node 3's right).
Hints
- Do a normal BFS, collecting each level's values into a buffer.
- Keep a boolean that flips every level; reverse the buffer on right-to-left levels before appending.
- DFS alternative: recurse with a depth index and append to result[depth], inserting at the front when depth is odd — but BFS is usually clearer.