Reported September 2026
Teslalinked list

Flatten a Branched List into a Doubly Linked List

Reported by candidates from Tesla's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Tesla OA. Under 2s to a working solution.
Founder's read

Tesla reported this one in September 2026, and the constraint is the whole story: n goes up to 100000, so recursion depth and repeated list walking will hurt you. The task is a preorder flatten of a branched list into a doubly linked list, returned as [value, prev, next] rows. It's a linked-list problem dressed in array indices. If you've seen LeetCode's flatten multilevel doubly linked list, you know the shape. The twist is that you output positions, not nodes. StealthCoder is there as a safety net if you blank during the live OA, but the logic below is short enough to carry in your head.

The problem

A branched list contains n nodes. Node i stores values[i], may point to a next node through nextIndices[i], and may point to one side branch through branchIndices[i]. A value of -1 means that pointer is absent.
Flatten the structure in preorder: visit a node, then its entire side branch, then the node's original next chain. Convert that order into one doubly linked list.
Return one row per flattened position. Row i is [value, previousPosition, nextPosition], where missing neighbors are encoded as -1.

Function
flattenBranchedList(values: int[], nextIndices: int[], branchIndices: int[], headIndex: int) → int[][]

Examples
Example 1
values = [1,2,3,4,5]
nextIndices = [1,2,-1,4,-1]
branchIndices = [-1,3,-1,-1,-1]
headIndex = 0
return = [[1,-1,1],[2,0,2],[4,1,3],[5,2,4],[3,3,-1]]
The branch rooted at node 3 is inserted after node 1 and before its original next node 2.
Example 2
values = [7]
nextIndices = [-1]
branchIndices = [-1]
headIndex = 0
return = [[7,-1,-1]]
A single node has neither a previous nor a next flattened neighbor.
Example 3
values = [10,20,30,40]
nextIndices = [1,-1,-1,-1]
branchIndices = [2,-1,3,-1]
headIndex = 0
return = [[10,-1,1],[30,0,2],[40,1,3],[20,2,-1]]
Nested side branches are completed before traversal resumes along an original next pointer.

Constraints
1 <= n <= 100000.
values.length = nextIndices.length = branchIndices.length = n.
Each pointer is -1 or a valid node index.
The nodes reachable from headIndex form an acyclic branched list, and every reachable node is visited exactly once.
-10^9 <= values[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you only need the preorder sequence. Run an iterative DFS with an explicit stack. Pop a node, append it to the output order, then push its next index first and its branch index second, so the branch pops first. Skip -1 values. That gives exactly: node, whole branch, then the original next chain. Once you have the order list, assign each node a position. Row i is [values[order[i]], i-1 or -1, i+1 or -1 if i is the last]. This is O(n) time and O(n) space. The common pitfall is recursion. With 100000 nodes in a long chain or deep nesting, recursive DFS can overflow the stack. Another pitfall is mixing up node index and flattened position in the output. Previous and next are positions, not original indices. If you freeze on the stack push order during the live OA, StealthCoder can hand you the iterative version as a hedge.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Flatten a Branched List into a 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Tesla's OA.

Tesla reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Flatten a Branched List into a Doubly Linked List FAQ

What's the trick to the Tesla flatten branched list problem?+

Use an iterative preorder traversal with a stack. Push next first, then branch, so the branch gets processed before the original next chain. Record the visit order, then build rows from positions in that order. No pointer surgery is needed since the output is just index rows.

Why does n up to 100000 matter here?+

It rules out recursion in many languages, since a deep branch or long chain can blow the call stack. It also rules out anything quadratic, like walking to the tail of each branch to splice it. A single stack-based pass in O(n) is the safe route.

What's the most common mistake on this problem?+

Returning original node indices as previous and next instead of flattened positions. Row i must reference positions in the new order. Build an order array first, then previous is i-1 and next is i+1, with -1 at the ends.

Is this similar to a known LeetCode problem?+

It's in the family of flatten a multilevel doubly linked list, which uses the same preorder idea. Here the input is index arrays and the output is position rows, so it's a variant, not an identical match. The traversal logic carries over directly.

How do I prepare for this in 48 hours?+

Write the iterative stack preorder once from scratch, then test it on the three examples, especially nested branches. Check a single node and a long chain with no branches. Confirm you handle -1 pointers and the headIndex start. That covers the edge cases the OA is likely to probe.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Tesla.

OA at Tesla?
Invisible during screen share
Get it