Sorted Doubly Linked List to Balanced BST In Place
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills most solutions on this Salesforce OA, reported September 2026, is the even-sized segment. Pick the upper middle instead of the lower middle and your link table won't match, even though the tree is still balanced. The task is converting a sorted doubly linked list into a height-balanced BST, reusing the nodes, with previous as left and next as right. It's a tree problem built on divide and conquer over index ranges. If you blank on the index math mid-assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the structure.
The problem
values[i] is the value of original doubly linked-list node i; nodes are linked in index order and values are nondecreasing. Reuse those nodes to form a height-balanced binary search tree, treating each node's previous pointer as its left child and next pointer as its right child. Do not create replacement tree nodes. Return one row per original node, in index order, as [nodeIndex, leftIndex, rightIndex, parentIndex]; use -1 for a missing link. For every even-sized segment, choose its lower-middle node as the root, making the serialization deterministic. Function buildBalancedBstLinks(values: int[]) → int[][] Examples Example 1 values = [1,2,3,4] return = [[0,-1,-1,1],[1,0,2,-1],[2,-1,3,1],[3,-1,-1,2]] Node 1 is the lower-middle root; every row identifies the reused original node and its final links. Example 2 values = [-10,-3,0,5,9] return = [[0,-1,1,2],[1,-1,-1,0],[2,0,3,-1],[3,-1,4,2],[4,-1,-1,3]] Node 2 is the root and both sides are recursively balanced. Constraints 1 <= values.length <= 200000 -1000000000 <= values[i] <= 1000000000 values is nondecreasing. The returned link table is the required serialization; it does not represent newly allocated tree nodes.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: you don't need pointers at all. Values are already sorted and nodes are indexed, so recurse on index ranges [lo, hi]. Root is mid = lo + (hi - lo) / 2, which is the lower middle for even sizes. That matches the spec. Left child is the root of [lo, mid-1], right child is the root of [mid+1, hi], and you record the parent as you go. Fill three arrays (left, right, parent) initialized to -1, then emit one row per index. The common pitfall is recursion depth and wrong middle choice. Depth is only about log n here, so recursion is safe even at 200000 nodes. Another pitfall is rebuilding the list or allocating new nodes, which the problem forbids and which wastes time. Total work is O(n) time and O(n) output. If the live OA freezes you on the base case or the parent assignment, StealthCoder is the hedge that keeps you moving.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Sorted Doubly Linked List to Balanced BST In Place 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as convert sorted list to binary search tree. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sorted Doubly Linked List to Balanced BST In Place FAQ
What's the trick in this Salesforce BST problem?+
Skip pointer manipulation. Recurse on index ranges, pick the lower middle as lo + (hi - lo) / 2, and record left, right, and parent arrays. The sorted order is already given by the indices, so no list traversal is needed.
Why does the lower-middle rule matter so much?+
The output is a deterministic link table that gets compared exactly. A different middle on even-sized segments still gives a balanced tree but different rows, so you'd fail the tests. Check Example 1: node 1 is the root of four nodes.
How hard is this really?+
Medium. It's the classic sorted-list-to-BST idea, but the output format adds bookkeeping. If you know index-range recursion, it's about 20 lines. The risk is off-by-one errors and the parent array, not the algorithm.
Will recursion blow the stack at 200000 nodes?+
No. A balanced tree has depth around 18 for 200000 nodes, so recursion depth stays tiny. The only deep-recursion danger is building a skewed tree, and this construction can't produce one.
How do I prepare for this in 48 hours?+
Write sorted-array-to-BST from memory, then extend it to record parent and child indices into arrays. Test on lengths 1, 2, 4, and 5 against the examples. Those cases cover the even-segment and single-node edge cases.