BST to Sorted Circular Doubly Linked List
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Up to 100000 nodes means a skewed BST can go 100000 levels deep, so the way you traverse matters. This is the Google BST to Sorted Circular Doubly Linked List problem, reported in September 2026. It's a tree problem at heart: inorder traversal while rewiring left and right pointers on the fly. The catch is the in-place rule and the circular wrap at the end. If you blank on the pointer juggling during the live assessment, StealthCoder runs invisibly as a safety net and hands you the structure. Better to know the trick first, though.
The problem
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]] 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 = [] An empty tree produces an empty serialization. Example 3 root = [7] return = [[7,7,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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: an inorder traversal of a BST visits nodes in ascending order. Keep a prev pointer and a head pointer. At each visited node, if prev exists, set prev.right = node and node.left = prev. Otherwise node is the head. After the traversal, link tail and head to close the circle: tail.right = head, head.left = tail. Then walk the circle from head and emit [val, left.val, right.val] rows. The common pitfall is recursion depth. With 100000 nodes on a skewed tree, plain recursion can overflow in some languages, so use an explicit stack. Another pitfall is forgetting the empty tree and the single node case, where the node points to itself. Don't allocate new nodes, and don't collect values into an array first. Time is O(n), extra space is O(h) for the stack. StealthCoder is your hedge on the live OA if the pointer wiring tangles, but this pattern is short once you see it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill BST to Sorted Circular Doubly Linked List cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as convert binary search tree to sorted doubly linked list. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
BST to Sorted Circular Doubly Linked List FAQ
What's the trick to BST to circular doubly linked list?+
Do an inorder traversal and stitch as you go. Track prev and head. For each node, link prev.right to node and node.left to prev. After the last node, connect tail and head to make it circular. No extra arrays needed.
How hard is this Google OA question really?+
Medium. The idea is simple if you know inorder on a BST gives sorted order. The difficulty is pointer bookkeeping, the circular closure, and edge cases. Most people lose time on wiring order, not on the algorithm itself.
Why does the 100000 node limit matter?+
A skewed BST can be 100000 levels deep, which risks stack overflow with naive recursion in some languages. An iterative inorder with an explicit stack avoids that. Either works if your language handles the depth, but the iterative version is the safe choice.
What edge cases should I test?+
Empty tree returns an empty matrix. A single node must point to itself for both previous and next, giving [7,7,7]. Also test a fully left-skewed and right-skewed tree, and make sure the output starts at the smallest node.
How do I prepare for this in 48 hours?+
Write inorder traversal both recursively and iteratively until you can do it blind. Then add the prev and head pointers and the final circular link. Run the three examples by hand. Practice serializing the circle by walking from head for n steps.