Question presented to candidate: "We get hundreds of price messages a second over a WebSocket. If we update the table for every message, the page stutters. Build a live ticker for five symbols that coalesces updates, for example once per animation frame or every 250 milliseconds, and show me how many renders actually happen for a burst of messages."
Requirements:
- A table of symbols with price, change since open and an up/down marker; a simulated feed (no network) delivering each message in its own task, as a socket would.
- A batching mode switch: per animation frame (
requestAnimationFrame), a fixed 250 ms throttle, and "off" (one update per message) for comparison. - Between flushes, keep only the latest message per symbol; apply the whole batch in one state update.
- A small heads-up display that counts messages, state updates and renders, without the counter itself causing renders.
- Stop timers and cancel a pending frame when the component unmounts.
What a strong answer should cover:
- Why per-message updates are expensive and why "latest value wins" is safe for prices (but not for an order book or trade log).
- Buffering in a non-reactive place (a ref, a plain
Map) and scheduling exactly one flush per window. requestAnimationFrameversus a fixed interval: matching the display refresh versus a predictable cadence and lower CPU in background tabs.- Comparing against the price the user last saw for the up/down arrow, not the previous message.
- Measuring render counts, and accessibility for fast-changing numbers (no
aria-livespam, colour plus a symbol).
Clarifying questions expected:
- "Is it fine to drop intermediate prices, or must every tick be shown (for example in a chart)?"
- "How many symbols and messages per second should this handle?"
- "Should updates pause when the tab is hidden?"
Code / implementation expected: Yes. A working ticker with the three modes and a render counter.