FastPrepConvert a Sorted Linked List to a Balanced BST
Problems/Convert a Sorted Linked List to a Balanced BST

Convert a Sorted Linked List to a Balanced BST

Meta logoMeta● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported from a Meta phone screen for a full-time software engineer role at level E6. The round had about 30 minutes of behavioral questions and about 30 minutes for two coding questions. This is the second coding question. The report names it only in a coded way, which we read as the standard task of turning a sorted singly linked list into a height-balanced binary search tree; the candidate says it was a known problem they had solved before. The code could not be run in the round, so the candidate explained the solution out loud.

The report gives no examples, limits or output rules. The usual task accepts any height-balanced tree. To give one exact answer, we chose the rule that every root is the middle node, taking the one closer to the front when there are two. We also chose strictly increasing values, the limits, the level-order output and every example.

Our version is about 85% the same as the reported question.

You are given the head of a singly linked list whose values are in strictly increasing order. Build a height-balanced binary search tree that holds every value of the list exactly once, and return its root.

In a binary search tree, every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. A tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most one.

Many trees meet these rules, but only one is accepted: the tree built this way. The root of any part of the list is the middle node of that part. If the part has an even number of nodes, it has two middle nodes; take the one closer to the front. The nodes before the root form its left subtree and the nodes after it form its right subtree, and each is built by the same rule. An empty list gives an empty tree.

In the examples, a tree is written level by level from left to right, with null marking a missing child; nulls after the last node are left out.

Function

sortedListToBST(head: ListNode) → TreeNode

Examples

Example 1

head = [-7,-2,4,8,15]return = [4,-7,8,null,-2,null,15]
head
-7-24815
return
4-7-2815

The list has five nodes, so its middle node 4 is the root. The nodes before it, -7, -2, are an even pair, so the one closer to the front, -7, roots the left subtree and -2 becomes its right child. In the same way 8 roots the right subtree and 15 is its right child. In level order this is [4,-7,8,null,-2,null,15].

Example 2

head = [1,2,3,4]return = [2,1,3,null,null,null,4]
head
1234
return
2134

Four nodes have two middle nodes, 2 and 3. The rule picks the one closer to the front, so 2 is the root. 1 is its left child. The right part, 3, 4, is again even, so 3 is its root and 4 hangs on its right.

Example 3

head = []return = []
head
empty
return
empty

An empty list gives an empty tree, shown as [].

Constraints

  • The list has between 0 and 1000 nodes.
  • -10^5 <= node.val <= 10^5
  • The values are in strictly increasing order.

More Meta Phone Screen questions

  • Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
  • First Element Greater Than KPHONE SCREEN · Seen Nov 2026
  • Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
  • Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
  • Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • Construct a Binary Tree from Preorder and InordereBay · PHONE SCREENeBay interview questions
  • Filter Data with Composable PredicatesCoreWeave · PHONE SCREEN
  • Trim a Binary Search Tree to a RangeGoogle · PHONE SCREENGoogle interview questions
  • Count Uni-Valued SubtreesAmazon · PHONE SCREEN
  • Reconstruct a BST from PreorderLyft · PHONE SCREENLyft interview questions
  • Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Rebuild a Binary Tree From Two WalksAdobe · PHONE SCREENAdobe interview questions
  • Swap Adjacent Linked List NodesNutanix · PHONE SCREENNutanix interview questions

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        // Write your code here.
    }
}
Checking account…