Question presented to candidate: "Build the comment section under a blog post. Comments can have replies, replies can have replies, with no fixed limit. I want to reply inline under any comment, collapse a thread to hide its replies, delete a comment together with everything under it, and add a new top-level comment."
Requirements:
- Render nested comments recursively, each reply indented under its parent with a thread line.
- Reply opens an inline textarea under that comment; Post is disabled while the text is empty or only spaces; Ctrl+Enter (or Cmd+Enter) posts; Escape or Cancel closes it.
- Collapse / expand a thread with a toggle button that has
aria-expanded; a collapsed comment shows how many replies are hidden. - Delete removes the comment and all its replies and announces how many were removed.
- A header shows the total comment count; a top-level form adds a new thread.
What a strong answer should cover:
- A recursive
Commentcomponent with a stablekeyper id and a depth for indentation. - Data vs UI state: the comment tree is data; "collapsed" and "replying" are per-comment UI state that can live in the component.
- Immutable tree helpers by id (
addReply,removeComment) or in-place mutation where the framework tracks it. - Counting a subtree recursively for the header and the "N replies hidden" note.
- Accessibility: real buttons,
aria-expanded, labelled textareas, a live status message.
Clarifying questions expected:
- "Is there a maximum depth, or do we indent forever?"
- "Should deleting keep a '[deleted]' placeholder when the comment has replies?"
- "Are comments loaded all at once, or replies fetched on demand?"
Code / implementation expected: Yes. A working recursive widget with reply, collapse and delete.