Running a machine-coding round · core
State design for UI components
Keep the smallest state that cannot be computed and derive everything else. Give each piece of state one owner, use a status value instead of several booleans, and move complex transitions into a pure reducer that you can test.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Minimal vs derived: store todos and filter; compute visible todos, counts and 'all done' during render.
- One status field ('idle' | 'loading' | 'success' | 'error') instead of isLoading + isError booleans that can contradict each other; in TypeScript, a discriminated union on that status string.
- Store ids, not copies: selectedId, not selectedItem (the copy goes stale after an edit).
- useReducer when several fields change together (forms, games, wizards); a pure reducer is testable in Node.
- Controlled (value + onChange) vs uncontrolled (defaultValue) component APIs; support both for reusable components.
- Normalise trees and boards: byId + childIds/cardIds.
- Immutable updates; functional setState (setCount(c => c + 1)) when the new value depends on the old, especially inside intervals and async callbacks.
- Keys are stable ids, never array indexes for lists that reorder, insert or delete.
The red pen
Common traps: mistake → fix
Mistake: Store the filtered list in state
Fix: it drifts from the source list.
Mistake: Mutate then set the same array
Fix: React sees the same reference and may skip the update.
Mistake: isLoading and isError as separate booleans
Fix: impossible states like both true.
Mistake: Index keys
Fix: inputs and checkboxes jump to the wrong row after a delete.
Easy to hard
Questions interviewers ask, easy to hard
- What state does this component need? What is derived?
- Why a reducer here?
- How would you support both controlled and uncontrolled use?
- You delete the first row and the checkbox moves to another row. Why?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.