Lesson 15 of 30
A chat UI looks simple until you build one — the state shape is a specific, learnable pattern, and most of the hard parts are UX details that never show up in a tutorial's happy path.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
As tokens stream in for an assistant's response, what's the correct way to update the messages array?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
The state at the heart of any chat interface is an array of messages, each with a role ("user" or "assistant", sometimes "system") and content. Everything the UI does is really just operations on that array:
type ChatMessage = {
id: string;
role: "user" | "assistant";
content: string;
status: "complete" | "streaming";
};
// Sending: append the user's message immediately, then start a streaming placeholder
function onSend(text: string, messages: ChatMessage[]): ChatMessage[] {
return [
...messages,
{ id: crypto.randomUUID(), role: "user", content: text, status: "complete" },
{ id: crypto.randomUUID(), role: "assistant", content: "", status: "streaming" },
];
}
// On each streamed token: append to the last message's content, don't replace the array
function onToken(token: string, messages: ChatMessage[]): ChatMessage[] {
const last = messages[messages.length - 1];
return [...messages.slice(0, -1), { ...last, content: last.content + token }];
}
This is the same shape this app's own AI call sites are built around: every request goes through lib/ai/chat/handler.ts's handleChat, which is the single entry point regardless of which UI surface (tutor, interview) is consuming the stream — the message-array-plus-streaming-append pattern above is what any UI built on top of it needs to implement.
"Design the state for a streaming chat interface" is a common practical/whiteboard question, and describing the message-array-plus-in-progress-message pattern, plus naming the scroll/cancel/disabled-input details unprompted, is what separates a candidate who's actually built one from someone describing it in the abstract.