Question presented to candidate: "Take a basic nested comments widget and make it feel like Reddit or Hacker News. People can upvote or downvote any comment and see the score change immediately, even though the server is slow and sometimes fails. The thread can be sorted by Best or New, clicking the vertical thread line collapses that branch, and very deep threads stop indenting and show a 'Continue this thread' link instead."
Requirements:
- Up and down vote buttons per comment with
aria-pressed; clicking the same arrow again removes the vote; switching arrows replaces it. - Optimistic update: the score changes at once, a simulated API (300 ms) confirms or fails; on failure the vote rolls back to the last confirmed value with a message.
- Out-of-order responses must not corrupt state: only the newest request for a comment may roll back.
- Sort Best (server score, ties older first) or New (newest first), applied at every level, without reshuffling rows when you vote.
- Collapse a thread from its
[-]toggle or by clicking its thread line; collapsed comments show how many replies are hidden. - Depth limit: below depth 3, show "Continue this thread (N more)", which opens that subtree on its own with a Back button and moves focus to the heading.
What a strong answer should cover:
- Separating server data (score) from the user's own vote, and showing
score + myVote. - Optimistic update with rollback to a confirmed value, plus a per-comment sequence number so a stale failure does not undo a newer vote.
- Sorting as derived data per level, and why ranking on the server score avoids rows jumping under the cursor.
- Lifting collapse state out of the components (a
Setof ids) so it survives re-sorting and the "continue thread" view. - Depth limiting and focus management when switching views; accessible toggles and thread lines.
Clarifying questions expected:
- "Can users vote on their own comments? Is a down vote allowed?"
- "Should a new vote re-rank the thread immediately, or only on reload?"
- "What should the user see when a vote fails: silent rollback or a message?"
Code / implementation expected: Yes. The vote flow with rollback and the stale-response guard is the core; sorting, collapse and the depth limit complete it.