Implement array parity and tree level views
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in array frequency analysis and binary tree traversal, testing skills in data structures, correctness reasoning, and algorithm implementation.
Part 1: Check Whether All Array Elements Appear an Even Number of Times
Constraints
- 0 <= len(nums) <= 100000
- -10^9 <= nums[i] <= 10^9
Examples
Input: []
Expected Output: True
Explanation: The array is empty, so there are no values with odd frequency.
Input: [2, 2, 3, 3, 4, 4]
Expected Output: True
Explanation: Each distinct number appears exactly twice.
Input: [1, 2, 2, 1, 3]
Expected Output: False
Explanation: 1 appears twice and 2 appears twice, but 3 appears once, which is odd.
Input: [-1, -1, -2, -2, -2, -2]
Expected Output: True
Explanation: -1 appears twice and -2 appears four times; both are even counts.
Input: [7]
Expected Output: False
Explanation: 7 appears once, which is an odd frequency.
Hints
- Try tracking which numbers currently have odd frequency as you scan the array.
- If a number is seen once, it becomes odd; if seen again, it becomes even.
Part 2: Right Side View of a Binary Tree
Constraints
- 0 <= len(tree) <= 10000
- -1000 <= node values <= 1000
- The input list follows standard level-order tree encoding with None for missing children
Examples
Input: ([1, 2, 3, None, 5, None, 4],)
Expected Output: [1, 3, 4]
Explanation: At each level, the rightmost visible nodes are 1, then 3, then 4.
Input: ([1, 2, None, 3],)
Expected Output: [1, 2, 3]
Explanation: The tree extends down the left side, so the visible nodes from the right are still one node per level: 1, 2, and 3.
Input: ([],)
Expected Output: []
Explanation: An empty tree has no visible nodes.
Input: ([7],)
Expected Output: [7]
Explanation: A single-node tree is visible as just that node.
Input: ([None],)
Expected Output: []
Explanation: A list starting with None represents an empty tree.
Input: ([-1, -2, -3, None, -5],)
Expected Output: [-1, -3, -5]
Explanation: The rightmost nodes by level are -1 at the root, -3 on the second level, and -5 on the third level.
Hints
- Process the tree one level at a time using a queue.
- At each level, the last node you visit is the one visible from the right.
Part 3: Left Side View of a Binary Tree
Constraints
- 0 <= len(tree) <= 10000
- -1000 <= node values <= 1000
- The input list follows standard level-order tree encoding with None for missing children
Examples
Input: ([1, 2, 3, None, 5, None, 4],)
Expected Output: [1, 2, 5]
Explanation: At each depth, the leftmost visible nodes are 1, then 2, then 5.
Input: ([1, 2, None, 3],)
Expected Output: [1, 2, 3]
Explanation: The tree goes down the left side as 1 -> 2 -> 3, so all three are visible.
Input: ([],)
Expected Output: []
Explanation: An empty tree has no visible nodes.
Input: ([7],)
Expected Output: [7]
Explanation: A tree with only a root shows just that root.
Input: ([0, -1, -1, None, -2],)
Expected Output: [0, -1, -2]
Explanation: The visible nodes from the left are the root 0, then the left child -1, then its right child -2.
Hints
- Use breadth-first search to process one level at a time.
- At each level, the first node you remove from the queue is the one visible from the left.
Part 4: Average Value of Each Level in a Binary Tree
Constraints
- 0 <= len(tree) <= 10000
- -100000 <= node values <= 100000
- The input list follows standard level-order tree encoding with None for missing children
Examples
Input: ([3, 9, 20, None, None, 15, 7],)
Expected Output: [3.0, 14.5, 11.0]
Explanation: Level 0 has [3], level 1 has [9, 20], and level 2 has [15, 7]. Their averages are 3.0, 14.5, and 11.0.
Input: ([5],)
Expected Output: [5.0]
Explanation: A single-node tree has one level, so the average is just the root value.
Input: ([],)
Expected Output: []
Explanation: An empty tree has no levels.
Input: ([1, None, 2, 3],)
Expected Output: [1.0, 2.0, 3.0]
Explanation: This compact level-order form means 2 is the right child of 1, and 3 is the left child of 2.
Input: ([-4, -2, -6, None, 0, 8],)
Expected Output: [-4.0, -4.0, 4.0]
Explanation: The levels are [-4], [-2, -6], and [0, 8], giving averages -4.0, -4.0, and 4.0.
Hints
- Breadth-first search naturally groups nodes by level.
- For each level, keep a running sum and divide by the number of nodes in that level.