Quick Overview

A coding problem that asks you to turn a sorted singly linked list into a height-balanced binary search tree and return it in level order, with a subtree size rule that makes the answer unique. It tests recursive construction over list ranges and careful tree serialization.

Build a Height-Balanced BST From a Sorted Singly Linked List

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given a singly linked list whose values are sorted in strictly increasing order. Build a height-balanced binary search tree that contains exactly those values, and return the tree in level order. The list is passed as a Python list of its node values from head to tail. A binary tree is height-balanced when, at every node, the heights of its two subtrees differ by at most one. Many height-balanced trees can hold the same values, so the size rule below fixes exactly one of them. ### Function Signature ```python def sorted_list_to_bst(values: list[int]) -> list[int | None]: ``` ### Rules - The returned tree must be a binary search tree whose in-order traversal equals `values`. - Size rule: at every node, the left subtree has either the same number of nodes as the right subtree or exactly one more. A tree that follows this rule is always height-balanced. - Output format: serialize the tree breadth-first. Start with the root; then, for each non-missing node in the order it was written, write its left child and then its right child, using `None` for a missing child. Remove all trailing `None` entries. An empty tree is `[]`. ### Constraints - `0 <= len(values) <= 2 * 10^4` - `-10^5 <= values[i] <= 10^5` - `values` is strictly increasing. ### Examples #### Example 1 ```text Input: values = [-10, -3, 0, 5, 9] Output: [0, -3, 9, -10, None, 5] ``` The root `0` has two nodes on each side. Node `-3` has left child `-10` and no right child, and node `9` has left child `5` and no right child; at both, the left side is one node larger, as the size rule allows. #### Example 2 ```text Input: values = [1, 2, 3, 4] Output: [3, 2, 4, 1] ``` The root `3` has two nodes on its left (`1` and `2`) and one on its right (`4`). Node `2` has left child `1`. #### Example 3 ```text Input: values = [] Output: [] ``` An empty list gives an empty tree.

Overview: A coding problem that asks you to turn a sorted singly linked list into a height-balanced binary search tree and return it in level order, with a subtree size rule that makes the answer unique. It tests recursive construction over list ranges and careful tree serialization.

Read the full Meta Software Engineer interview experience this question came from

You are given a singly linked list whose values are sorted in strictly increasing order, passed as a list of its node values from head to tail. Build a height-balanced binary search tree that contains exactly those values, and return the tree in level order. A binary tree is height-balanced when, at every node, the heights of its two subtrees differ by at most one. Many height-balanced trees can hold the same values, so the size rule below fixes exactly one of them. ### Rules - The returned tree must be a binary search tree whose in-order traversal equals `values`. - Size rule: at every node, the left subtree has either the same number of nodes as the right subtree or exactly one more. A tree that follows this rule is always height-balanced. - Output format: serialize the tree breadth-first. Start with the root; then, for each non-missing node in the order it was written, write its left child and then its right child, using `None` for a missing child. Remove all trailing `None` entries. An empty tree is `[]`. The `None` marker is written as `null` in JavaScript, as a `null` entry of the returned `java.util.List<Integer>` in Java, and as `std::nullopt` in the returned `std::vector<std::optional<int>>` in C++. Every value fits in a 32-bit signed integer. ### Constraints - `0 <= len(values) <= 2 * 10^4` - `-10^5 <= values[i] <= 10^5` - `values` is strictly increasing. ### Example 1 ```text Input: values = [-10, -3, 0, 5, 9] Output: [0, -3, 9, -10, None, 5] ``` The root `0` has two nodes on each side. Node `-3` has left child `-10` and no right child, and node `9` has left child `5` and no right child; at both, the left side is one node larger, as the size rule allows. ### Example 2 ```text Input: values = [1, 2, 3, 4] Output: [3, 2, 4, 1] ``` The root `3` has two nodes on its left (`1` and `2`) and one on its right (`4`). Node `2` has left child `1`.

Constraints

  • 0 <= len(values) <= 2 * 10^4
  • -10^5 <= values[i] <= 10^5
  • values is strictly increasing.

Examples

Input: ([],)

Expected Output: []

Explanation: An empty list gives an empty tree.

Input: ([7],)

Expected Output: [7]

Explanation: A single value is the root; its two missing children are trailing None entries and are removed.

Hints

  1. For a list of length n, work out how many values the size rule places in the left subtree and how many in the right; that count tells you which value sits at the root.
  2. Every subtree is built from a contiguous slice of the list, and the same rule applies inside it.
  3. In the output, only nodes that exist contribute child entries, and trailing None entries are dropped at the end.

Size-Balanced BST from a Sorted Linked List in Level Order

hardMetaSoftware Engineer

You are given a singly linked list whose values are sorted in strictly increasing order, passed as a list of its node values from head to tail. Build a height-balanced binary search tree that contains exactly those values, and return the tree in level order.

A binary tree is height-balanced when, at every node, the heights of its two subtrees differ by at most one. Many height-balanced trees can hold the same values, so the size rule below fixes exactly one of them.

Rules

  • The returned tree must be a binary search tree whose in-order traversal equals values.
  • Size rule: at every node, the left subtree has either the same number of nodes as the right subtree or exactly one more. A tree that follows this rule is always height-balanced.
  • Output format: serialize the tree breadth-first. Start with the root; then, for each non-missing node in the order it was written, write its left child and then its right child, using None for a missing child. Remove all trailing None entries. An empty tree is [].

The None marker is written as null in JavaScript, as a null entry of the returned java.util.List<Integer> in Java, and as std::nullopt in the returned std::vector<std::optional<int>> in C++. Every value fits in a 32-bit signed integer.

Constraints

  • 0 <= len(values) <= 2 * 10^4
  • -10^5 <= values[i] <= 10^5
  • values is strictly increasing.

Example 1

Input:  values = [-10, -3, 0, 5, 9]
Output: [0, -3, 9, -10, None, 5]

The root 0 has two nodes on each side. Node -3 has left child -10 and no right child, and node 9 has left child 5 and no right child; at both, the left side is one node larger, as the size rule allows.

Example 2

Input:  values = [1, 2, 3, 4]
Output: [3, 2, 4, 1]

The root 3 has two nodes on its left (1 and 2) and one on its right (4). Node 2 has left child 1.

Examples

Example 1
Input
([],)
Output
[]
Notes
An empty list gives an empty tree.
Example 2
Input
([7],)
Output
[7]
Notes
A single value is the root; its two missing children are trailing None entries and are removed.

Constraints

  • •0 <= len(values) <= 2 * 10^4
  • •-10^5 <= values[i] <= 10^5
  • •values is strictly increasing.

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
Signature
def sorted_list_to_bst(values: list[int]) -> list[int | None]:
Sample call
sorted_list_to_bst([])
Language
Case 1
Input
([],)
Expected
[]
Case 2
Input
([7],)
Expected
[7]

Browse More Questions

Show the approach

Approach

For a contiguous range values[lo:hi] holding n = hi - lo values, the size rule allows only one split: the left subtree gets n // 2 values and the right subtree gets n - n // 2 - 1. These are equal when n is odd and differ by one in favor of the left when n is even. Because the in-order traversal must equal the range, the root is values[mid] with mid = lo + n // 2 = (lo + hi) // 2, the left subtree is built from values[lo:mid] and the right subtree from values[mid + 1:hi]. Applying this at every node yields the single tree the rules allow.

The tree is never built as linked nodes. A breadth-first queue holds half-open index ranges and starts with (0, n). Popping a nonempty range writes values[mid] and appends its left range and then its right range; popping an empty range writes None and appends nothing, so children of missing nodes are never written. The queue order is the serialization order because every written node adds its two children left first, in the order the nodes were written. After the queue empties, the trailing None entries are removed.

Each value is written once and adds two ranges, so the queue holds at most 2n + 1 ranges and the work is linear. Edge cases: an empty list writes a single None that the trim removes, giving []; one value gives [v]; two values give [values[1], values[0]] after the root's missing right child is trimmed.

Time complexity:
O(n)
Space complexity:
O(n)