Convert a Sorted Linked List of Timestamps Into a Balanced BST
Last updated: November 29, 2026
Quick Overview
Given a singly linked list of timestamps in ascending order, build a height-balanced binary search tree containing them, in O(n) time and without copying the list into an array.
0
0
0 solved
Given a singly linked list of timestamps in ascending order, build a height-balanced binary search tree containing them, in O(n) time and without copying the list into an array.
A Google onsite follow-up to the sorted-array version. Finding the middle with slow and fast pointers at every level is O(n log n); the interviewer is looking for the in-order simulation that consumes the list once.
What the Interviewer Expects
- Note that the array approach needs random access the list lacks
- Explain why repeated middle-finding is O(n log n)
- Build the tree in in-order sequence while advancing a list pointer
- Use the list length to size each subtree
- State O(n) time and O(log n) recursion depth
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the list is doubly linked and you must reuse its nodes as tree nodes?
- Handle a stream where the length is unknown in advance.
- Convert the BST back into a sorted circular doubly linked list in place.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: head of a sorted singly linked list. Output: root of a height-balanced BST with the same values. O(n) time is required; O(n) extra array space is not allowed....
High-Level Design
In-order traversal of a BST visits keys in sorted order, which is exactly the order of the list. So build the tree in in-order: count the length n once, then define build(size) which builds a subtre...