PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates a candidate's understanding of binary tree traversal and array local-minimum detection, measuring skills in data structures (binary trees and arrays), algorithm design, and algorithmic time-complexity analysis.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Implement right side view and local minimum search

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question LeetCode 199. Binary Tree Right Side View — Given the root of a binary tree, return the values of the nodes you can see ordered from top to bottom when the tree is viewed from the right side. Given an unsorted array of distinct integers, design an algorithm that returns the index of any local minimum (an element strictly smaller than its immediate neighbors). Assume the virtual elements beyond the ends are +∞. Provide the algorithm and analyze its time complexity. https://leetcode.com/problems/binary-tree-right-side-view/description/

Quick Answer: This question evaluates a candidate's understanding of binary tree traversal and array local-minimum detection, measuring skills in data structures (binary trees and arrays), algorithm design, and algorithmic time-complexity analysis.

Given a binary tree represented by its level-order array (use null for missing children) and an array of distinct integers, implement a function that returns two results: (1) the right side view of the tree (the values visible from the right, top to bottom), and (2) the index of any local minimum in the array. A local minimum is an element strictly smaller than its immediate neighbors; elements beyond the ends are treated as +∞. Use 0-based indexing. Return the results as [right_view_list, local_min_index]. If the tree is empty, the right view is []. The array length is at least 1.

Constraints

  • 0 <= len(tree) <= 200000
  • 1 <= len(nums) <= 200000
  • All elements in nums are pairwise distinct
  • Tree values fit in 32-bit signed integer range
  • Return value format: [right_view_list, local_min_index]

Solution

from typing import List, Optional, Any
from collections import deque

class TreeNode:
    __slots__ = ("val", "left", "right")
    def __init__(self, val: int, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def build_tree(level: List[Optional[int]]) -> Optional[TreeNode]:
    """Builds a binary tree from level-order array with None as null."""
    if not level:
        return None
    it = iter(level)
    root_val = next(it)
    if root_val is None:
        return None
    root = TreeNode(root_val)
    q = deque([root])
    while q:
        node = q.popleft()
        try:
            lv = next(it)
        except StopIteration:
            break
        if lv is not None:
            node.left = TreeNode(lv)
            q.append(node.left)
        try:
            rv = next(it)
        except StopIteration:
            break
        if rv is not None:
            node.right = TreeNode(rv)
            q.append(node.right)
    return root


def right_side_view(root: Optional[TreeNode]) -> List[int]:
    if root is None:
        return []
    res: List[int] = []
    q = deque([root])
    while q:
        size = len(q)
        for i in range(size):
            node = q.popleft()
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)
            if i == size - 1:
                res.append(node.val)
    return res


def find_local_min_index(nums: List[int]) -> int:
    """Binary search for any local minimum index with virtual +∞ boundaries."""
    n = len(nums)
    l, r = 0, n - 1
    while l < r:
        m = (l + r) // 2
        if nums[m] > nums[m + 1]:
            l = m + 1
        else:
            r = m
    return l


def right_view_and_local_min(tree: List[Optional[int]], nums: List[int]) -> List[Any]:
    root = build_tree(tree)
    rv = right_side_view(root)
    idx = find_local_min_index(nums)
    return [rv, idx]
Explanation
Convert the level-order array (with nulls) into a binary tree by iterating the list and linking children in breadth-first order. To compute the right side view, perform a level-order traversal (BFS) and record the last node value in each level. For the local minimum, use a binary search on the array's slope: if nums[mid] > nums[mid+1], a local minimum exists in the right half; otherwise, it exists in the left half (including mid). Distinct values and virtual +∞ on both ends ensure the existence of a local minimum and correctness of the binary search.

Time complexity: O(m + log n), where m is the number of non-null tree nodes and n is len(nums). Space complexity: O(w) auxiliary for BFS (w = maximum tree width) and O(1) for the local minimum search; building the tree uses O(m) space for nodes.

Hints

  1. For the right side view, process the tree level by level (BFS) and take the last node value of each level.
  2. Alternatively, a DFS that visits right children before left and records the first node seen at each depth also works.
  3. For the local minimum, use binary search on the slope: compare nums[mid] and nums[mid+1] to decide which half contains a local minimum.
  4. Distinct elements and virtual +∞ boundaries guarantee that a local minimum exists.
  5. Handle edge cases: empty tree (view is []), single-element array (index 0).
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Maze and Suffix Problems - Meta (medium)