← Questions

Build a chat input that renders a streamed AI reply

0
OpenAICodingLast asked Apr 2026

A coding interview question asked in OpenAI's technical screen (coding), last asked Apr 2026. It tests frontend, streaming and react.

Build a bare-bones AI assistant chat page: a text input, a submit button, and below them the assistant's reply, which streams in chunk by chunk as it arrives rather than appearing all at once. Use the framework you know best (React is the usual choice) and be ready to defend it.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • POST /api/chat with body { prompt } returns the reply as a stream of plain-text chunks over fetch (a ReadableStream body), then completes or errors. It accepts an AbortSignal.
  • You may wrap it as streamChat(prompt, { signal }) or read the stream directly; say which.
  • A reply is dozens to hundreds of small chunks over a few seconds. One page, no router, no global state library, no auth or persistence.

Requirements:

  1. Render the input and the button.
  2. On submit, call the API with the prompt.
  3. Show the reply as chunks arrive.
  4. Keep the page responsive while streaming: the input doesn't freeze, nothing janks.
  5. Block or handle a second submit while a reply is still streaming.
  6. Show loading and error states.

Example

type "Hi", submit      status: loading      button disabled
chunk "Hel"            status: streaming    reply: "Hel"
submit again           ignored              reply: "Hel"
chunk "lo"             status: streaming    reply: "Hello"
stream ends            status: done         reply: "Hello"   button enabled

If the stream errors mid-reply, the partial text stays and an error with Retry appears.

Clarifying questions

  • Which transport and chunk format: a raw body stream, Server-Sent Events, or a WebSocket? Plain text or JSON events with an end marker?
  • Can the user stop a reply in progress?
  • One prompt and one reply, or a scrolling multi-turn transcript?
  • On an error mid-stream: keep the partial text, discard it, or offer a retry?
  • Accessibility needs? Does Enter submit, and is the input cleared on submit?

What a strong answer covers

  • One status value (idle | loading | streaming | done | error) plus the prompt and the accumulated reply, not several booleans that can contradict each other.
  • A reader loop with one TextDecoder used with { stream: true }, so a multi-byte character split across two reads decodes correctly.
  • Duplicate-submit protection at the UI (disabled button) and an AbortController kept for the in-flight request, aborted on unmount or Stop; an abort is not shown as an error.
  • A request id or ref checked after each await, so a slow chunk from an old request can't write into a newer reply, and no state updates after unmount.
  • Render cost: batch chunks into one update per animation frame (or a ref plus scoped update) instead of a full re-render per token; why the read loop doesn't block the main thread.
  • Tests that feed a mocked chunk stream deterministically.

Follow-ups

  • A multi-turn transcript with a scrolling history, keeping the streaming bubble fast.
  • A Stop generating button: what state changes, and how stale chunks are ignored afterwards.
  • Very long replies: auto-scroll only while the user is near the bottom, and don't yank them down after they scroll up.
  • Make the streaming text accessible to screen readers.

Where it's asked

  • OpenAI · Technical screen: coding · Last asked Apr 202660 min · One problem in three to five growing parts filling the hour, usually in CoderPad; ML roles get a Colab notebook with autocomplete off · No AI assistants or autocomplete; documentation is fine

What it tests

frontend · streaming · react · ui

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding