PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates algorithmic problem-solving across arrays, binary trees, and grid pathfinding, testing competencies in array traversal and visibility reasoning, vertical column grouping and ordering in trees, and reachability/search in 2D grids.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Solve array, tree, and maze problems

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

The onsite interview included the following algorithmic tasks: 1. **Building visibility from the right**: Given an integer array `heights`, where `heights[i]` is the height of the `i`th building in a row, return the indices of buildings that can see the ocean on the right. A building can see the ocean if every building to its right is strictly shorter. Return the indices in increasing order. 2. **Binary tree vertical grouping**: Given the root of a binary tree, return the node values grouped by vertical column from left to right. Nodes in the same column should be reported from top to bottom. If multiple nodes share the same row and column, output them in the order they are visited by a breadth-first traversal. 3. **Maze pathfinding in an AI-assisted coding round**: Given a 2D grid where `0` represents an open cell and `1` represents a wall, along with a start cell and a target cell, determine whether there is a valid path from start to target by moving up, down, left, or right.

Quick Answer: This question evaluates algorithmic problem-solving across arrays, binary trees, and grid pathfinding, testing competencies in array traversal and visibility reasoning, vertical column grouping and ordering in trees, and reachability/search in 2D grids.

Part 1: Building Visibility from the Right

You are given an integer array `heights`, where `heights[i]` is the height of the `i`th building in a row. The ocean is to the right of the last building. A building can see the ocean if every building to its right is strictly shorter. Return the indices of all buildings that can see the ocean, in increasing order.

Constraints

  • 0 <= len(heights) <= 100000
  • 1 <= heights[i] <= 1000000000

Examples

Input: [4, 2, 3, 1]

Expected Output: [0, 2, 3]

Explanation: Building 3 sees the ocean. Building 2 is taller than building 3, so it also sees the ocean. Building 1 is blocked by building 2. Building 0 is taller than every building to its right.

Input: [4, 3, 2, 1]

Expected Output: [0, 1, 2, 3]

Explanation: Each building is taller than all buildings to its right, so every building can see the ocean.

Input: []

Expected Output: []

Explanation: There are no buildings, so the result is an empty list.

Input: [2, 2, 2]

Expected Output: [2]

Explanation: Only the last building can see the ocean. The others are blocked because buildings to their right are not strictly shorter.

Input: [7]

Expected Output: [0]

Explanation: A single building always sees the ocean.

Input: [2, 1, 2, 1]

Expected Output: [2, 3]

Explanation: Building 3 sees the ocean. Building 2 is taller than building 3, so it also sees. Building 1 is blocked by building 2, and building 0 is blocked by building 2 because equal height does not count as strictly shorter.

Hints

  1. Try scanning from right to left instead of checking every building's entire suffix.
  2. Keep track of the tallest building seen so far from the right.

Part 2: Binary Tree Vertical Grouping

You are given a binary tree and must group its node values by vertical column, from left to right. Nodes in the same column should appear from top to bottom. If multiple nodes share the same row and the same column, they must be reported in the order they are visited by a breadth-first traversal. For this standalone problem, the tree is provided as a level-order list using `None` for missing children.

Constraints

  • 0 <= len(values) <= 10000
  • Each non-None value is an integer in the range [-10000, 10000]
  • The input list is a valid level-order encoding of a binary tree

Examples

Input: ([3, 9, 20, None, None, 15, 7],)

Expected Output: [[9], [3, 15], [20], [7]]

Explanation: Node 9 is in the left column, nodes 3 and 15 share the middle-left column from top to bottom, node 20 is next, and node 7 is the rightmost.

Input: ([1, 2, 3, 4, 6, 5, 7],)

Expected Output: [[4], [2], [1, 6, 5], [3], [7]]

Explanation: Nodes 6 and 5 share the same row and column, so they are reported in breadth-first order: 6 before 5.

Input: ([],)

Expected Output: []

Explanation: An empty tree has no vertical columns.

Input: ([1],)

Expected Output: [[1]]

Explanation: A single-node tree has exactly one column.

Input: ([1, 2, 3, None, 4, 5, None],)

Expected Output: [[2], [1, 4, 5], [3]]

Explanation: Nodes 4 and 5 both land in the root's column and are listed in breadth-first visitation order.

Hints

  1. Assign a column index to each node: root = 0, left child = column - 1, right child = column + 1.
  2. A breadth-first traversal naturally preserves the required order for nodes that land in the same row and column.

Part 3: Maze Pathfinding

You are given a 2D grid where `0` represents an open cell and `1` represents a wall. You are also given a start cell and a target cell. Determine whether there is a valid path from start to target by moving one step at a time up, down, left, or right. If the grid is empty, a position is out of bounds, or either the start or target is a wall, return `False`.

Constraints

  • 0 <= number of rows, number of columns <= 200
  • Each cell in `grid` is either 0 or 1
  • Movement is allowed only in the four cardinal directions

Examples

Input: ([ [0, 0, 1], [1, 0, 1], [1, 0, 0] ], (0, 0), (2, 2))

Expected Output: True

Explanation: One valid path is (0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2).

Input: ([ [0, 1, 0], [1, 1, 0], [0, 0, 0] ], (0, 0), (2, 2))

Expected Output: False

Explanation: The start cell is boxed in, so no path exists.

Input: ([[0]], (0, 0), (0, 0))

Expected Output: True

Explanation: The start and target are the same open cell.

Input: ([], (0, 0), (0, 0))

Expected Output: False

Explanation: An empty grid cannot contain a valid path.

Input: ([ [0, 1], [0, 0] ], (0, 1), (1, 1))

Expected Output: False

Explanation: The start position is a wall, so the path is invalid immediately.

Hints

  1. Model the open cells as a graph and run a traversal from the start cell.
  2. Keep a visited set so you do not revisit cells and loop forever.
Last updated: May 6, 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 Two Backtracking Array Problems - Meta (hard)
  • Solve Array, Matrix, and Recommendation Problems - Meta (medium)
  • Find a String Containing Another - Meta (medium)
  • Solve Subarray Sum and Local Minimum - Meta (hard)
  • Validate abbreviations and brackets - Meta (medium)