Render a Comment Hierarchy
Updated
Problem statement
You are given three arrays of the same length. Together they describe a flat list of comments:
ids[i]is the unique ID of commenti.parentIds[i]is-1when commentiis a root, which means it has no parent. Otherwise it is the ID of that comment's parent.texts[i]is the comment text.
A comment's children are the comments whose parent is that comment. Following parent links never makes a loop, and replies can be nested to any depth. A parent may appear before or after its children in the input.
List the comments in preorder: write a comment, then each of its children the same way, before moving on. Start with the roots. Keep input order among root comments and among comments that share the same parent.
Return one string per comment. A root has depth zero, and a child is one level deeper than its parent. A comment at depth d becomes exactly 2 * d spaces followed by its text.
Function
renderCommentHierarchy(ids: int[], parentIds: int[], texts: String[]) → String[]Examples
Example 1
ids = [10,20,30,40,50]parentIds = [-1,10,10,20,-1]texts = ["Root A","Reply A1","Reply A2","Deep reply","Root B"]return = ["Root A"," Reply A1"," Deep reply"," Reply A2","Root B"]Root A comes first. Its children, Reply A1 and Reply A2, follow in input order. Deep reply is the child of Reply A1, so it goes right after Reply A1. The two replies to Root A have depth one and get two leading spaces each. Deep reply has depth two, so it gets four. The second root, Root B, comes last. So the answer is Root A, Reply A1, Deep reply, Reply A2, Root B, with those spaces.
Example 2
ids = [4,2,1,3]parentIds = [2,1,-1,1]texts = ["deep","first child","root","second child"]return = ["root"," first child"," deep"," second child"]root appears after deep and first child in the input, but the parent IDs still rebuild the tree. The root's children are comment 2 and comment 3, kept in input order. deep is the child of first child, so it comes right after first child, one level deeper. So the answer is root, first child, deep, second child, each indented by its depth.
Example 3
ids = [5,6,7]parentIds = [-1,-1,6]texts = ["alpha","beta","gamma"]return = ["alpha","beta"," gamma"]alpha and beta are both roots, so they keep their input order. Comment 7, gamma, is the only child of beta. So the answer is alpha, beta, and then gamma indented by its depth.
Constraints
0 <= ids.length = parentIds.length = texts.length <= 100000.- Every value in
idsis unique and is not-1. - Every
parentIds[i]is either-1or a value present inids. - The parent relationships contain no cycle.
- Every value in
textsis a non-null string.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions