Coding questions · Meta
Solve Tree View and Triplet Sum
This multipart question evaluates proficiency with tree-based data structures and array-based algorithmic problem solving, focusing on reasoning about which nodes are visible from a binary tree's right side and identifying unique zero-sum triplets in an integer array; it tests concepts such as tree traversal, duplicate handling, and algorithmic complexity analysis. These problems are commonly asked to assess algorithmic problem-solving ability, manipulation of fundamental data structures, and attention to edge cases and performance, and they fall under the Coding & Algorithms category with a focus on practical application of algorithmic concepts supported by conceptual understanding of data structures and complexity.
You are asked to solve two coding problems.
Problem 1: Return the nodes visible from the right
Given the root of a binary tree, return the values of the nodes that would be visible if the tree were viewed from its right side. Return the values from top to bottom.
Example:
Input:
1
/ \
2 3
\ \
5 4
Output: [1, 3, 4]
Constraints:
- The number of nodes is in the range
[0, 10^4]. - Node values are integers.
Problem 2: Find unique triplets with zero sum
Given an integer array nums, return all unique triplets [a, b, c] such that a + b + c = 0. The solution set must not contain duplicate triplets. The order of triplets and the order of numbers inside each triplet do not matter.
Example:
Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Constraints:
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5
Part 1: Right Side View of a Binary Tree
You are given a binary tree serialized as a Python list in level-order, where None represents a missing child. Return the values of the nodes visible when the tree is viewed from the right side, from top to bottom.
Constraints
- The number of nodes is in the range [0, 10^4].
- Node values are integers.
- The input list is a valid level-order serialization of a binary tree.
Noneindicates that a child is missing.
Examples
Example 1
Input: [1, 2, 3, None, 5, None, 4]
Output: [1, 3, 4]
Explanation: From the right, you see 1 at level 0, 3 at level 1, and 4 at level 2.
Example 2
Input: [1, 2, 3, 4, None, None, None]
Output: [1, 3, 4]
Explanation: Although 4 is on the left subtree, it is still the only visible node at its depth from the right.
Hints
- A level-order traversal lets you process one depth level at a time.
- At each level, the last node you visit is the one visible from the right.
Part 2: Find Unique Triplets With Zero Sum
Given an integer array nums, return all unique triplets [a, b, c] such that a + b + c = 0. The result must not contain duplicate triplets. For deterministic grading, return each triplet in nondecreasing order, and return the full list of triplets in lexicographic order.
Constraints
- 3 <= nums.length <= 3000
- -10^5 <= nums[i] <= 10^5
- The solution set must not contain duplicate triplets.
Examples
Example 1
Input: [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]
Explanation: These are the only two unique triplets whose sum is zero.
Example 2
Input: [0, 0, 0, 0]
Output: [[0, 0, 0]]
Explanation: Even though there are many ways to pick three zeros, the triplet should appear only once.
Hints
- Sorting the array turns the problem into finding pairs with a target for each fixed first number.
- After finding a valid triplet, move both pointers and skip repeated values to avoid duplicates.