Question presented to candidate: "Let us start simple. Build me a counter: it shows a number that starts at 0, with a plus button, a minus button and a reset button. It should stay between 0 and 10, and I want a small input to change how much each click adds. Walk me through your state as you go."
Requirements:
- Show the current count, starting at the
initialvalue (default 0). - + adds the step, โ subtracts it, Reset goes back to the initial value.
- Respect
minandmax(0 and 10 here): the count never leaves the range, and the buttons show they cannot go further. - A step input: a whole number of 1 or more; anything else (empty, 2.5, -3) falls back to 1.
- Accessible: real buttons with clear names, the new count announced to screen readers, keyboard friendly.
What a strong answer should cover:
- The minimal state: the count and the raw step text. Everything else (the parsed step, "at min", "at max", the status message) is derived during render, not stored.
- Functional updates (
setCount(c => ...), or the framework equivalent) so rapid clicks always build on the latest value. - Clamping in one place, so a step of 4 from 9 lands on 10 instead of 13 or a no-op.
- Why
aria-disabledcan be kinder thandisabledon a button that becomes unavailable under the user's focus, plusaria-livefor the changing number. - Props with sensible defaults (
initial,min,max) so the component is reusable.
Clarifying questions expected:
- "At the limit, should a big step clamp to the bound or be refused?"
- "Should Reset also reset the step?"
- "Is the count owned by this component, or does a parent need to read it?"
Code / implementation expected: Yes. A working component in the framework of choice, roughly 40-60 lines, with bounds, step and reset.