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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
binary search tree
linked list
in-order construction
divide and conquer
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported