Flat Comments to a Nested Tree
Updated
Problem statement
Convert a flat list of comments into a nested JSON forest. Comment i has the string fields ids[i], parentIds[i], and texts[i]. An empty parent ID marks a root; otherwise, the comment is a child of the comment whose ID equals its parent ID.
Preserve input order among roots and among siblings. Return a compact JSON array. Every object must contain the fields id, parent_id, text, and children, in that order. The children value is an array that follows the same rule. Escape strings according to JSON.
Function
buildCommentTree(ids: String[], parentIds: String[], texts: String[]) → StringExamples
Example 1
ids = ["1","2","3","4","5"]parentIds = ["","","1","2","3"]texts = ["A","B","C","D","E"]return = "[{\"id\":\"1\",\"parent_id\":\"\",\"text\":\"A\",\"children\":[{\"id\":\"3\",\"parent_id\":\"1\",\"text\":\"C\",\"children\":[{\"id\":\"5\",\"parent_id\":\"3\",\"text\":\"E\",\"children\":[]}]}]},{\"id\":\"2\",\"parent_id\":\"\",\"text\":\"B\",\"children\":[{\"id\":\"4\",\"parent_id\":\"2\",\"text\":\"D\",\"children\":[]}]}]"Comments 1 and 2 are roots. Comment 3 is under 1, comment 5 is under 3, and comment 4 is under 2.
Example 2
ids = ["c","r","s"]parentIds = ["r","","r"]texts = ["first","root","second"]return = "[{\"id\":\"r\",\"parent_id\":\"\",\"text\":\"root\",\"children\":[{\"id\":\"c\",\"parent_id\":\"r\",\"text\":\"first\",\"children\":[]},{\"id\":\"s\",\"parent_id\":\"r\",\"text\":\"second\",\"children\":[]}]}]"A child may appear before its parent in the flat input. The two children of r retain their relative input order.
Constraints
0 <= ids.length <= 100000.- The three arrays have equal length.
- IDs are unique nonempty printable ASCII strings of length at most
50. - Each parent ID is empty or names exactly one other comment.
- The parent relationships form a forest.
- Text has at most
200printable ASCII characters.
More ByteDance Phone Screen questions
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Flat Records to a Hierarchical TreeAddepar · PHONE SCREEN
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Count Repeat Customers Across DatesAmazon · PHONE SCREEN
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Delete Selected Nodes from a Parent-Array ForestSnowflake · PHONE SCREENSnowflake interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience