Skip to the page

Trees, comments and drag and drop · mid

Nested comments

Nested comments turn a flat list with parentId into a tree in O(n) with a Map, render it recursively with a depth limit, keep one 'replying to' id in state, soft-delete comments that have replies, and load more replies per parent with a cursor.

The notes

What you must know

  • API data is usually flat ({ id, parentId, text, author, createdAt }); build the tree with a Map id → node in one pass (O(n)), or keep it normalised (byId + childIds) for easy updates.
  • Recursive <Comment> with indentation; cap visual depth and show 'Continue this thread' beyond it.
  • UI state: replyingToId (one open reply box), editingId, collapsed Set of ids; drafts kept per comment id (Adobe asked for auto-saved drafts).
  • Add reply: insert into the parent's children (sorted by time or score); optimistic with a temp id, replaced by the server id.
  • Delete: if a comment has replies, soft-delete ('[deleted]') to keep the thread; otherwise remove.
  • Votes/likes optimistic with rollback; sort by top or new.
  • Loading: top-level comments paginated; 'Load 5 more replies' per parent with its own cursor.
  • Accessibility: nested ul/li lists, buttons with clear labels ('Reply to Asha'), focus the new reply's textarea when the box opens.

The red pen

Common traps: mistake → fix

  • Mistake: Find children with filter() inside recursion

    Fix: O(n²) for big threads.

  • Mistake: Hard-delete a parent

    Fix: its replies disappear or become orphans.

  • Mistake: One global 'reply text' state

    Fix: typing in one box changes the others.

  • Mistake: Unlimited indentation

    Fix: unreadable on phones.

Easy to hard

Questions interviewers ask, easy to hard

  1. Build Reddit-style nested comments.
  2. The API returns a flat list with parentId. Build the tree efficiently.
  3. Add edit and delete. What happens to replies when the parent is deleted?
  4. Threads are 10,000 comments. How do you load and render them?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview