FastPrepBST to Sorted Circular Doubly Linked List
Problems/BST to Sorted Circular Doubly Linked List

BST to Sorted Circular Doubly Linked List

Google logoGoogle● MediumNEW GRADINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks you to convert a binary search tree in place into a sorted circular doubly linked list. The links follow inorder order, and the last node must link back to the first. To make the circle safe to check, we give the result as a finite matrix. Our version is about 96% the same as the reported question.

You are given the root of a binary search tree with distinct values. Convert the tree in place into a sorted circular doubly linked list:

  • Reuse each node's left pointer as previous.
  • Reuse each node's right pointer as next.
  • Link nodes in ascending order.
  • Join the smallest and largest nodes so the list is circular.

Do not allocate replacement list nodes. Return a serialization of the converted list beginning at its smallest node. Each output row is [value, previousValue, nextValue]. An empty tree returns an empty matrix. Storage for the returned rows is excluded from the in-place conversion requirement.

Function

bstToCircularDoublyList(root: TreeNode) → int[][]

Examples

Example 1

root = [4,2,5,1,3]return = [[1,5,2],[2,1,3],[3,2,4],[4,3,5],[5,4,1]]
root
42135

Inorder traversal is 1, 2, 3, 4, 5. The first node's previous pointer wraps to 5, and the last node's next pointer wraps to 1.

Example 2

root = []return = []
root
empty

An empty tree produces an empty serialization.

Example 3

root = [7]return = [[7,7,7]]
root
7

In a one-node circle, both previous and next point back to the same node.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -1000000000 <= node.val <= 1000000000.
  • All node values are distinct.
  • The input satisfies the binary-search-tree ordering invariant.
  • The conversion must reuse the original tree nodes; recursion or an explicit traversal stack is allowed.
  • Output serialization storage is excluded from the in-place conversion requirement.

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • BST to Sorted Circular Doubly Linked ListApple · ONSITE INTERVIEWApple interview questions
  • BST to Sorted Circular Doubly Linked ListBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Convert a Binary Tree to a Doubly Linked ListExpedia · ONSITE INTERVIEWExpedia interview questions
  • Convert a BST to a Doubly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Turn a BST Into a Sorted Doubly Linked ListAdobe · PHONE SCREENAdobe interview questions
  • Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
  • Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
  • Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
/**
 * 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 int[][] bstToCircularDoublyList(TreeNode root) {
        // Write your code here.
    }
}
Checking account…