Question presented to candidate: "After sign-in we text the user a 6-digit code. Build the input for it: six separate boxes, one digit each. Typing should jump to the next box, Backspace should feel natural, and pasting the whole code from the clipboard must work. When all six digits are in, verify the code against the server."
Requirements:
- Six single-digit boxes; non-digits are ignored.
- Typing a digit moves focus to the next box; typing into a filled box replaces its digit.
- Backspace clears the current box, or, on an empty box, clears and focuses the previous one. ArrowLeft and ArrowRight move between boxes.
- Pasting (or SMS autofill dropping the whole code into one box) fills the boxes from the cursor, skipping spaces and dashes.
- When the sixth digit lands, call a (simulated) verify API once; disable the boxes while it runs; on a wrong code, clear the boxes and focus the first one.
- Accessible: a labelled group, a label per box, the numeric keypad on phones, and
autocomplete="one-time-code".
What a strong answer should cover:
- The state model: one array of strings as the single source of truth, plus refs to the DOM inputs used only to move focus.
- Firing "complete" from the event handler that filled the last box, not from an effect that watches the array.
- Keyboard edge cases: Backspace on an empty box, replacing a digit, ignoring letters, arrow keys.
- Paste and autofill handling with
preventDefault, and whymaxLength={1}breaks them. - Accessibility:
role="group"with a label,aria-labelper box,inputMode="numeric",autocomplete="one-time-code", arole="status"message. - How the parent resets the widget after a wrong code (a
keychange in React and Vue, a method call through@ViewChildin Angular).
Clarifying questions expected:
- "Digits only, or alphanumeric codes too? Is the length always 6?"
- "Should we submit automatically on the last digit, or wait for a Verify button?"
- "What should happen after a wrong code: keep the digits for editing, or clear them?"
Code / implementation expected: Yes. A working component in the candidate's framework with typing, Backspace, arrows, paste and a simulated verify call; tests or a driver script are a bonus.