Question presented to candidate: "Build the message pane of a support chat. Messages come in over a socket, the user can type and send, and the list should scroll to new messages, but not if the user has scrolled up to read history. Show the message immediately when sent, and handle the case where sending fails."
Requirements:
- A scrollable conversation with sent and received bubbles and timestamps, plus a composer: Enter sends, Shift+Enter adds a new line, the Send button is disabled for empty input.
- A simulated socket (no network) that acknowledges sends, shows a typing indicator, replies, and can push unprompted incoming messages.
- Optimistic send: the message appears at once as "Sending...", becomes "sent" with a server time on ack, or "Failed to send" with a Retry button on error.
- Smart auto-scroll: stick to the bottom only if the user is already near it; otherwise show an "N new messages" button. Your own send always scrolls down.
- Stable list keys across the pending to sent transition.
What a strong answer should cover:
- A message model with a client id, an optional server id and a status (
pending,sent,failed). - Why the client id stays the key after the server id arrives (no remount, no lost focus or animation).
- Detecting "at bottom" from
scrollHeight - scrollTop - clientHeightwith a threshold, stored without causing renders. - Scrolling after the DOM has updated (layout effect, post-flush watcher, after-render effect).
- Accessibility:
role="log"for the conversation, a labelled textarea, failures announced, IME composition not sending early.
Clarifying questions expected:
- "Is message order guaranteed by the server, or do we sort by server timestamp?"
- "Do we need history loading when scrolling to the top, or only live messages?"
- "Should a failed message retry automatically, or only when the user asks?"
Code / implementation expected: Yes. A working chat pane with the simulated socket, optimistic send, retry and conditional auto-scroll.