← Machine Coding

Nested Comments

medium
Asked at
ByteDance
Facebook
LinkedIn
Meta
ByteDance, Facebook, LinkedIn, Meta

Problem statement

Build this React exercise in 40 minutes.

Render the supplied finite comment tree with globally unique IDs and a replies array on every node. Add trimmed nonempty replies at the end of the chosen parent’s children. A root-owned immutable tree stores content; each comment controls its editor and collapse state. Collapse hides descendants without destroying their drafts or expansion state. Cancel hides the editor and keeps its draft; posting clears that editor and reveals its direct replies. No API, deletion, moderation, or unlimited-depth performance is required.

Acceptance checks

  • Posting to the third-level comment appends there, preserving sibling data and the original input tree.
  • Blank/whitespace replies are blocked; reply text renders as text, including <script> strings.
  • Collapse an ancestor and reopen it: descendant draft and collapse state remain intact.
  • Cancel/reopen retains a draft; posting clears that draft and expands the parent’s replies.
  • Repeated posts use unique IDs; controls expose expanded state and labeled textareas. Unknown parent ID leaves the tree unchanged.

The checks are a manual interview checklist; saving a submission does not execute them.

Learn and continue

Prerequisite: Immutable state. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements & constraints

  • →Posting to the third-level comment appends there, preserving sibling data and the original input tree.
  • →Blank/whitespace replies are blocked; reply text renders as text, including