Question presented to candidate: "Our team-lunch poll shows results live as colleagues vote. Build the widget: results update by themselves, your vote appears instantly and is saved to the server, a failed save rolls back cleanly, the widget remembers that you voted after a reload, and a successful vote gets a little confetti burst. No animation libraries."
Requirements:
- Load the poll from a simulated server (no network); show a loading state.
- Subscribe to live votes from other people (pushed as "someone voted for X") and update counts, percentages and total as they arrive; unsubscribe on unmount.
- One-click voting on an option: optimistic +1, pressed and disabled states, a "Saving your vote..." status.
- On server failure: undo only your own +1 (keep live votes that arrived meanwhile), show an error, re-enable voting. A "make the next vote fail" toggle demonstrates it.
- On success: persist the chosen option in
localStorageunder the poll id, restore it on mount, and play a hand-made confetti burst (about 30 CSS-animated pieces, removed afterwards, skipped for reduced motion).
What a strong answer should cover:
- Why rollback must apply an inverse delta rather than restore a snapshot when live updates are flowing.
- Subscribing after the initial snapshot loads, guarding against unmount before load, and cleaning up the subscription (including under React StrictMode).
- Persisting only what the server accepted, with
localStoragewrapped in try/catch. - A confetti effect built from spans, CSS custom properties and one keyframe;
aria-hidden,pointer-events: none, cleanup of its timer and aprefers-reduced-motioncheck. - Accessible live data: announce your own result and errors, not every pushed vote.
Clarifying questions expected:
- "Does the server push deltas or full snapshots? That changes how rollback and reconciliation work."
- "Should results be visible before voting, or hidden to avoid bias?"
- "Is localStorage enough to stop repeat votes, or does the server enforce one vote per user?"
Code / implementation expected: Yes. A working widget with the simulated live server, optimistic vote with rollback, persistence and confetti.