Facebook Pixel

How to Build Nested Comments in an Interview

Reddit-style nested comments with reply, vote, and collapse. Here is how to build it.

How to Build Nested Comments in an Interview

Nested comments (Reddit-style) test recursion and tree rendering. Here is how to build it.

Requirements

  • Display threaded comments with indentation.
  • Reply to any comment (adds a nested child).
  • Upvote/downvote.
  • Collapse/expand a comment's children.
  • Handle deeply nested structures.

Data Structure

const comments = [ { id: 1, text: "Great post!", votes: 5, children: [ { id: 2, text: "Thanks!", votes: 2, children: [] }, { id: 3, text: "I agree", votes: 1, children: [] }, ], }, ];

Recursive Rendering

function renderComment(comment, depth) { const div = document.createElement("div"); div.className = "comment"; div.style.marginLeft = depth * 20 + "px"; div.innerHTML = ` <div class="comment-header"> <button class="vote-up" data-id="${comment.id}">+</button> <span class="votes">${comment.votes}</span> <button class="vote-down" data-id="${comment.id}">-</button> <button class="reply" data-id="${comment.id}">Reply</button> <button class="collapse" data-id="${comment.id}">Collapse</button> </div> <p>${comment.text}</p> <div class="children" id="children-${comment.id}"></div> `; const childrenContainer = div.querySelector(`#children-${comment.id}`); comment.children.forEach((child) => { childrenContainer.appendChild(renderComment(child, depth + 1)); }); return div; }

Event Handling (Event Delegation)

document.getElementById("commentsContainer").addEventListener("click", (e) => { const id = e.target.dataset.id; if (e.target.classList.contains("vote-up")) updateVote(id, 1); if (e.target.classList.contains("vote-down")) updateVote(id, -1); if (e.target.classList.contains("reply")) showReplyForm(id); if (e.target.classList.contains("collapse")) toggleCollapse(id); });

CSS

.comment { border-left: 2px solid #ccc; padding: 8px; margin: 8px 0; } .comment-header { display: flex; gap: 8px; align-items: center; } .votes { font-weight: bold; min-width: 30px; } button { cursor: pointer; padding: 2px 8px; }

Edge Cases

  • Empty children (no replies).
  • Very deep nesting (limit depth or add "load more").
  • Reply form (inline text area).
  • Collapse state persistence.
  • Vote updates (re-render or update DOM directly).

The Takeaway

Build nested comments with a recursive render function, tree data structure, event delegation for vote/reply/collapse, and CSS indentation with border-left. Handle empty children, deep nesting, and reply forms. This tests recursion, tree rendering, and event delegation.

Use a tree data structure. Write a recursive render function that creates a div for each comment and recursively renders children. Add vote, reply, and collapse buttons. Use event delegation for clicks. Indent with marginLeft based on depth.

A tree: each comment is an object with id, text, votes, and children (array of comment objects). Children can have their own children, forming a tree. Render recursively.

Show an inline form (textarea) when reply is clicked. On submit, add a new comment object to the parent's children array. Re-render the children container. Use a unique ID (incrementing counter or UUID).

Toggle the display of the children container: childrenContainer.style.display = isVisible ? 'none' : 'block'. Update the button text (Collapse/Expand). Use event delegation to handle the click.

Empty children (no replies), very deep nesting (limit depth or add 'load more replies'), reply form (inline textarea), collapse state persistence, and vote updates. Use event delegation for efficient event handling.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.