Bobyard logoBobyard
CodingFree preview

Turning 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.

SWE
medium
tree
hashmap
frontend
react
javascript
Frequency
Single report
Last asked
—
Stage
onsite-coding

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: null is a root.
  • Each other comment is attached under the comment whose id equals its parentId.
  • 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.

Hints

Pitfalls

  • Scanning the array for each parent (find inside 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 id and content into the node breaks the renderer as soon as it needs createdAt, 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.

Talking Points

  1. Announce the two-pass structure before coding: indexing by id turns an O(n^2) problem into O(n) and shows deliberate optimization.
  2. Use a Map rather than a plain object, because ids can be numbers, strings or UUIDs and there are no prototype-key surprises.
  3. Make the orphan decision explicit (promote, skip or throw) and justify promoting as avoiding data loss.
  4. Spread the original comment so every field the API returns is preserved.
  5. Mention recursion depth for rendering and say you would switch to an explicit stack for pathological inputs.
  6. Sorting belongs after construction because it is a presentation concern; sort each children array too if needed.

Follow-ups

Preparation

  • Implement buildCommentTree from 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 children array is sorted by createdAt, and state the resulting complexity.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...