Compute tree right view and target-sum subarrays
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This two-part question evaluates skills in tree traversal and visible-node extraction for the binary-tree right-side view task and in array cumulative-sum reasoning for counting contiguous subarrays with a target sum, assessing data-structure manipulation and algorithmic problem-solving competency.
Part 1: Right-Side View of a Binary Tree
Constraints
- 0 <= number of list entries <= 10^4
- Node values are 32-bit signed integers
- The input list is a valid level-order encoding of a binary tree
- The tree may be empty and does not need to be balanced
Examples
Input: [10, 5, 20, 3, 7, None, 30]
Expected Output: [10, 20, 30]
Explanation: The rightmost node at each depth is 10, then 20, then 30.
Input: [1, 2, 3, None, 5, None, 4]
Expected Output: [1, 3, 4]
Explanation: At level 2, node 3 hides node 2 from the right. At level 3, node 4 is the rightmost visible node.
Input: [42]
Expected Output: [42]
Explanation: A single-node tree is fully visible from the right side.
Input: []
Expected Output: []
Explanation: An empty tree has no visible nodes.
Input: [1, 2, None, 3, None, 4]
Expected Output: [1, 2, 3, 4]
Explanation: With only one node at each level, every node is visible from the right.
Solution
def solution(level_order):
from collections import deque
if not level_order or level_order[0] is None:
return []
class TreeNode:
__slots__ = ('val', 'left', 'right')
def __init__(self, val):
self.val = val
self.left = None
self.right = None
root = TreeNode(level_order[0])
queue = deque([root])
i = 1
while queue and i < len(level_order):
node = queue.popleft()
if i < len(level_order):
left_val = level_order[i]
i += 1
if left_val is not None:
node.left = TreeNode(left_val)
queue.append(node.left)
if i < len(level_order):
right_val = level_order[i]
i += 1
if right_val is not None:
node.right = TreeNode(right_val)
queue.append(node.right)
right_view = []
queue = deque([root])
while queue:
level_size = len(queue)
rightmost = None
for _ in range(level_size):
node = queue.popleft()
rightmost = node.val
if node.left is not None:
queue.append(node.left)
if node.right is not None:
queue.append(node.right)
right_view.append(rightmost)
return right_view
Time complexity: O(n). Space complexity: O(n).
Hints
- A breadth-first traversal lets you process the tree level by level; the last node you visit in each level is the visible one.
- An alternative is depth-first search that visits right children before left children and records the first node seen at each depth.
Part 2: Count Subarrays With a Given Sum
Constraints
- 0 <= n <= 10^5
- Each element of nums is a 32-bit signed integer
- k is a 32-bit signed integer
- An O(n) or near-O(n) solution is expected
Examples
Input: ([1, 2, 3], 3)
Expected Output: 2
Explanation: The valid subarrays are [1, 2] and [3].
Input: ([1, 1, 1], 2)
Expected Output: 2
Explanation: The subarrays [1, 1] at indices (0,1) and (1,2) both sum to 2.
Input: ([1, -1, 0], 0)
Expected Output: 3
Explanation: The valid subarrays are [1, -1], [0], and [1, -1, 0].
Input: ([], 0)
Expected Output: 0
Explanation: An empty array has no subarrays.
Input: ([0, 0, 0], 0)
Expected Output: 6
Explanation: All 6 contiguous subarrays sum to 0.
Solution
def solution(nums, k):
prefix_counts = {0: 1}
prefix_sum = 0
total = 0
for num in nums:
prefix_sum += num
total += prefix_counts.get(prefix_sum - k, 0)
prefix_counts[prefix_sum] = prefix_counts.get(prefix_sum, 0) + 1
return total
Time complexity: O(n). Space complexity: O(n).
Hints
- Track a running prefix sum as you scan the array.
- If `current_sum - k` has appeared before, then each previous occurrence marks a subarray ending at the current index with sum `k`.