Quick Overview

This question evaluates proficiency in data structures and algorithm design by testing binary search tree reconstruction from a preorder sequence and matrix spiral (clockwise layer) traversal.

Construct a BST and read spiral order

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

The coding round reportedly included two algorithmic tasks: 1. **Rebuild a binary search tree from a preorder sequence** - You are given an array of distinct integers representing the preorder traversal of a binary search tree. - Reconstruct the original tree and return its root. - Aim for a solution that runs in linear time. 2. **Traverse a matrix in clockwise layers** - You are given an `m x n` integer matrix. - Return all elements in the order obtained by repeatedly visiting the current outer boundary clockwise and then shrinking the boundary until every cell has been visited.

Overview: This question evaluates proficiency in data structures and algorithm design by testing binary search tree reconstruction from a preorder sequence and matrix spiral (clockwise layer) traversal.

Rebuild BST From Preorder

Return a nested [value,left,right] BST reconstructed from distinct preorder values.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([8,5,1,7,10,12],)

Expected Output: [8, [5, [1, None, None], [7, None, None]], [10, None, [12, None, None]]]

Explanation: Rebuild BST from preorder using bounds.

Input: ([],)

Expected Output: None

Explanation: Empty preorder returns None.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.

Clockwise Matrix Layer Traversal

Return matrix elements in clockwise spiral layer order.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

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

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

Explanation: Clockwise layer traversal.

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

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

Explanation: Single row works.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.

Rebuild BST From Preorder

Part 1: MediumMetaSoftware Engineer

Return a nested [value,left,right] BST reconstructed from distinct preorder values.

View full question (all parts)

The coding round reportedly included two algorithmic tasks:

  1. Rebuild a binary search tree from a preorder sequence

    • You are given an array of distinct integers representing the preorder traversal of a binary search tree.
    • Reconstruct the original tree and return its root.
    • Aim for a solution that runs in linear time.
  2. Traverse a matrix in clockwise layers

    • You are given an m x n integer matrix.
    • Return all elements in the order obtained by repeatedly visiting the current outer boundary clockwise and then shrinking the boundary until every cell has been visited.

Examples

Example 1
Input
([8,5,1,7,10,12],)
Output
[8, [5, [1, None, None], [7, None, None]], [10, None, [12, None, None]]]
Notes
Rebuild BST from preorder using bounds.
Example 2
Input
([],)
Output
None
Notes
Empty preorder returns None.

Constraints

  • •Inputs are Python literals matching the function signature.
  • •Return a deterministic exact-match value.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Sample call
solution([8,5,1,7,10,12])
Language
Case 1
Input
([8,5,1,7,10,12],)
Expected
[8, [5, [1, None, None], [7, None, None]], [10, None, [12, None, None]]]
Case 2
Input
([],)
Expected
None

Browse More Questions