Back to problems

Format a Forest of Nested Comments

Algorithm · Amazon · Hard

You are asked to render a collection of comment records as an ASCII-style tree. Each record has an integer id, a nullable parentId, and a string text. A record whose parentId is null is a top-level comment; otherwise it is a reply to the record whose id equals its parentId. The records are supplied in no particular hierarchical order, but their array order determines sibling order: if two records share the same parent, or both are top-level, the one appearing earlier in the…

Checking your access…