Question Bank
/

Tree-from-Flat-Array Challenges

Tree-from-Flat-Array Challenges

Three drills on shape conversion: building a nested tree from a flat list (numeric ids), building from a nested list with string parents, and assembling a graph adjacency list.

Question Bank
Hard
JavaScript
quiz
tree-traversal
recursion
array-manipulation-patterns

1,131 views

31

Implement transformData(data) that groups by parent id (parentFeatureId === 0 marks roots) and returns { rootName: { parent, features } }.

Examples

Example 1:

Input: transformData([{ id: 1, name: 'A', parentFeatureId: 0, level: 1 }, { id: 2, name: 'A-1', parentFeatureId: 1, level: 2 }])
Output: { A: { parent: { id: 1, ... }, features: [{ id: 2, ... }] } }
Explanation: First pass identifies parents; second pass attaches level-2 children by `parentFeatureId`.

2 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium