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

BST to Sorted Circular Doubly Linked List

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time onsite interview. The report asks for a conversion done in place. The links follow sorted inorder order, the two ends link to each other, and an empty tree gives null. Here the returned circle is written out as a finite matrix so it can be checked safely.

You are given the root of a binary search tree. All its values are distinct. Turn the tree into a sorted circular doubly linked list, in place:

  • Use each node's left pointer as its previous pointer.
  • Use each node's right pointer as its next pointer.
  • Link the nodes in ascending order of value.
  • Link the smallest and largest nodes to each other so the list forms a circle.

Do not create new nodes for the list. Reuse the tree's own nodes.

Return the converted list as rows, starting at its smallest node. Each row is [value, previousValue, nextValue]. The original task returns null for an empty tree. Here, return an empty matrix instead. The memory for the returned rows does not count against the in-place rule.

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

Visiting the tree in order (inorder traversal) gives 1,2,3,4,5. The first node's previous pointer wraps around to 5, and the last node's next pointer wraps around to 1.

Example 2

root = []return = []
root
empty

An empty tree returns an empty matrix.

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 has between 0 and 100000 nodes.
  • -1000000000 <= node.val <= 1000000000.
  • All node values are distinct.
  • The input is a valid binary search tree.
  • The conversion must reuse the original tree nodes. You may use recursion or your own stack to walk the tree.
  • The memory for the returned rows does not count against the in-place rule.

More Apple Onsite Interview questions

  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
  • Classify Natural-Month Distance Between DatesONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • BST to Sorted Circular Doubly Linked ListGoogle · ONSITE INTERVIEWGoogle 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

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple 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…