KatInterview

Live-Streaming Prompt Box with Incremental Reply Rendering

Data Structure DesignModel answer
MediumModel answerData Structure Designfrontendreactstreaming

Last updated

Design and code a small front-end component that sends a prompt, renders a streamed reply as it arrives, blocks duplicate sends, supports cancel, and shows loading and error states.

Build a minimal chat-style front-end widget, a stripped-down AI assistant. A user types a prompt and submits it. The assistant's answer must appear progressively, piece by piece, beneath the input, instead of showing up only once the whole reply is ready.

You have a backend endpoint that returns its answer as a stream of text chunks. You decide which transport to assume (fetch with a readable body, Server-Sent Events, or WebSocket) and you must state that assumption. For example: POST /api/chat with body { prompt }, which streams plain-text chunks and then either closes cleanly or errors. You may wrap it in a helper such as streamChat(prompt, { signal }) or read the stream directly in the component.

Requirements

  1. Show a text input and a submit button.
  2. On submit, call the streaming endpoint with the prompt.
  3. Display the reply below the input as each chunk arrives.
  4. Keep the UI responsive during the stream: the input stays usable and the page does not stutter, even with dozens to hundreds of small chunks over a few seconds.
  5. Prevent, or cleanly handle, duplicate submissions while a reply is still streaming.
  6. Show a loading state and an error state.

Scope

  • One self-contained component. No router. A global state library is not required, but be ready to say whether you would use one and why.
  • Target modern evergreen browsers. Use the framework you know best (React is a fine default).
  • Persistence, auth, markdown rendering and multi-turn history are out of scope unless you want to add them. Focus on the streaming render loop.

What to deliver

  • The component code (state model, submit handler, stream-reading loop, render).
  • A short explanation of your assumed API contract, your state transitions, and how you handle cancellation, cleanup, mid-stream errors and render performance.
  • The clarifying questions you would raise (transport and chunk format, cancel support, single-turn vs transcript, what to do with partial text on error, accessibility, Enter-to-submit behavior).

Hints

Hint 1

Model the UI with an explicit status value (idle, loading, streaming, done, error) plus the prompt and the accumulated reply text, so impossible combinations cannot occur.

Hint 2

Read the body with a reader loop and one reused TextDecoder using `{ stream: true }` so multi-byte characters split across chunks decode correctly. Wire an AbortController to both submit and unmount.

Hint 3

Appending to state on every chunk can cause hundreds of re-renders. Buffer chunks in a ref and flush once per animation frame, and guard against stale chunks from an earlier request after a new submit or cancel.