Flatten a Multilevel Doubly Linked List
Updated
Problem statement
Given a multilevel doubly linked list stored in arrays, flatten it into one level and return the flattened list, one row per node.
The list has n nodes, numbered by index. Node i holds values[i]. nextIndices[i] is the next node on the same level, and childIndices[i] is the head of the node's child list. A pointer value of -1 means there is no such node. headIndex is the first node of the top level. Each level is doubly linked: a node's previous node is the one whose next pointer points to it.
Flatten in preorder: a node comes first, then its whole child list (flattened the same way), then the node's original next node. Afterward no node has a child, and all nodes form one valid doubly linked list.
Row p describes position p of the flattened list and is [value, previousPosition, nextPosition]: the node's value and the positions of the nodes before and after it. Use -1 when there is no previous or next node. If the list is empty, return an empty matrix.
Function
flattenMultilevelList(values: int[], nextIndices: int[], childIndices: int[], headIndex: int) → int[][]Examples
Example 1
values = [1,2,3,4,5,6,7,8,9,10,11,12]nextIndices = [1,2,3,4,5,-1,7,8,9,-1,11,-1]childIndices = [-1,-1,6,-1,-1,-1,-1,10,-1,-1,-1,-1]headIndex = 0return = [[1,-1,1],[2,0,2],[3,1,3],[7,2,4],[8,3,5],[11,4,6],[12,5,7],[9,6,8],[10,7,9],[4,8,10],[5,9,11],[6,10,-1]]The child list that starts at value 7 goes right after value 3. Inside it, the child list 11, 12 goes right after value 8, then the walk goes on to 9 and later back up to 4.
Example 2
values = [1,2,3]nextIndices = [1,-1,-1]childIndices = [2,-1,-1]headIndex = 0return = [[1,-1,1],[3,0,2],[2,1,-1]]The child with value 3 goes right after its parent and before the parent's original next node, value 2.
Example 3
values = []nextIndices = []childIndices = []headIndex = -1return = []The list is empty, so the result is an empty matrix.
Constraints
0 <= n <= 1000.values.length = nextIndices.length = childIndices.length = n.- Every pointer is
-1or a valid node index. - If
n = 0, thenheadIndex = -1. OtherwiseheadIndexis a valid index. - The nodes you can reach have no cycles, and every reachable node appears exactly once.
1 <= values[i] <= 10^5.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- LRU CacheGimlet Labs · PHONE SCREEN
- LRU CacheBNSF Railway · PHONE SCREENBNSF Railway interview questions
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- Max Frequency TrackerProcol · PHONE SCREEN
Interview reports that mention this question
- Bloomberg New Grad Question Bank: 161 Questions (Part 1 of 2)
- Bloomberg New Grad Interviews: 2025 vs 2026 Guide
- Bloomberg New Grad VOs: 32 Experiences (Part 5 of 5)
- Bloomberg New Grad Screens: 30 Experiences (Part 4 of 5)
- Bloomberg New Grad OOD Follow-ups: 14 Experiences (Part 3 of 5)
- Bloomberg New Grad SWE Offer Loops: 14 Experiences (Part 2 of 5)