Build a chat input that renders a streamed AI reply
I was asked this
No reports yet. Sign in to say where you were asked it.
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/chatwith body{ prompt }returns the reply as a stream of plain-text chunks overfetch(aReadableStreambody), then completes or errors. It accepts anAbortSignal. - 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:
- Render the input and the button.
- On submit, call the API with the prompt.
- Show the reply as chunks arrive.
- Keep the page responsive while streaming: the input doesn't freeze, nothing janks.
- Block or handle a second submit while a reply is still streaming.
- 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
TextDecoderused with{ stream: true }, so a multi-byte character split across two reads decodes correctly. - Duplicate-submit protection at the UI (disabled button) and an
AbortControllerkept 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
