FastPrepConvert a BST to a Doubly Linked List
Problems/Convert a BST to a Doubly Linked List

Convert a BST to a Doubly Linked List

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Applied & Data Scientist internship onsite asks candidates to convert a BST into a doubly linked list. Linear output, ascending in-order nodes, distinct keys, empty behavior, portable index-linked representation, interface, and bounds are conventional enrichment.

Given a binary search tree root, convert its nodes into a linear doubly linked list in ascending key order.

Return a portable representation of the list: row i is [value, prevIndex, nextIndex], rows appear from head to tail, and -1 represents a missing neighbor. Return an empty matrix for an empty tree.

Function

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

Examples

Example 1

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

In-order traversal yields 1,2,3,4,5; the second and third columns encode each node's previous and next list indexes.

Example 2

root = []return = []
root
empty

An empty tree produces an empty list representation.

Constraints

  • The tree contains at most 100000 nodes
  • -10^9 <= node.val <= 10^9
  • All node values are distinct
  • The input satisfies the binary-search-tree ordering property

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

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

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft 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[][] bstToDoublyLinkedList(TreeNode root) {
      // Write your code here.
    }

}
Checking account…