BobyardTurning a Flat Comment List into Threaded Replies
Convert a flat list of comments carrying parentId values into a nested tree where each comment has a children array, then render that tree with indentation by depth. Roots are shown newest first.
Problem
The API returns comments as a flat array in which each record carries an optional parentId. Convert it into a nested structure where every comment has a children array, and then render the nested result in a UI with visible indentation.
- A comment with
parentId: nullis a root. - Each other comment is attached under the comment whose
idequals itsparentId. - All original fields (
content,createdAt, and so on) must survive the conversion. - Roots are presented newest first by
createdAt. - Decide, and state, what happens to a comment whose parent is not in the input.
Examples
Input:
[
{"id": 1, "content": "root", "parentId": null},
{"id": 2, "content": "reply to root", "parentId": 1},
{"id": 3, "content": "reply to reply", "parentId": 2}
]Output: a single root (id 1) whose children holds comment 2, whose children holds comment 3.
The component that renders the tree is a recursive React component taking comment and depth, which indents each level by a fixed number of pixels.
In the live round the starting point is the comment system from the take-home: the comment data now arrives with an extra parentId field, and the task is to render it as nested comments. No backend change is required; mock data in the frontend is acceptable. The round is pure coding with no conversation, timeboxed at about 45 minutes.
Hints
Pitfalls
- Scanning the array for each parent (
findinside a loop) quietly makes the solution quadratic. - Linking in a single pass fails when a reply appears before its parent in the array; that is why the index is built first.
- Copying only
idandcontentinto the node breaks the renderer as soon as it needscreatedAt, author or votes. - Dropping orphans loses user content.
- Mutating the input objects instead of copying them can surprise callers.
- Using a plain object as the index risks key problems such as
__proto__. - Pathologically deep threads can overflow the call stack in a recursive renderer.
- AI tools are not allowed in this round (Google is), and the interviewer, sometimes the CTO, watches you code live and talks fast. Practice building the tree and the recursive renderer without autocomplete.
- An unfinished solution is not automatically a rejection: candidates who explained their approach clearly but did not finish have still advanced.
Talking Points
- Announce the two-pass structure before coding: indexing by
idturns anO(n^2)problem intoO(n)and shows deliberate optimization. - Use a
Maprather than a plain object, because ids can be numbers, strings or UUIDs and there are no prototype-key surprises. - Make the orphan decision explicit (promote, skip or throw) and justify promoting as avoiding data loss.
- Spread the original comment so every field the API returns is preserved.
- Mention recursion depth for rendering and say you would switch to an explicit stack for pathological inputs.
- Sorting belongs after construction because it is a presentation concern; sort each
childrenarray too if needed.
Follow-ups
Preparation
- Implement
buildCommentTreefrom memory in under 10 minutes, including the orphan branch, and test it with a child listed before its parent. - Write the iterative renderer with an explicit
{ node, depth }stack and compare output with the recursive version. - Extend the builder so every
childrenarray is sorted bycreatedAt, and state the resulting complexity.
