Katkat

Solve Tree View and Triplet Sum

MetaSoftware Engineer · Technical Screen

Last updated

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