Frontend School

Robust Multi-Digit Verification Input

Machine Coding
medium

Robust Multi-Digit Verification Input

Medium
React
TypeScript
UI/UX
Forms
Accessibility

Overview

In many secure applications, multi-factor authentication (MFA) relies on One-Time Passcodes (OTPs) sent to a user's device. A smooth and intuitive OTP input experience is critical for user satisfaction and security. This problem challenges you to build a highly usable and accessible component for entering multi-digit verification codes, mirroring the quality expected in high-stakes user flows.

What you need to do / Task

Your goal is to implement a reusable React component, OTPInput, that facilitates the entry of a multi-digit verification code.

  1. Component Structure: Create a functional React component named OTPInput that accepts a length prop (number, e.g., 6) and an onChange prop (function).
  2. Individual Inputs: Render length individual input fields, each designed to accept a single digit.
  3. Auto-Focus & Navigation:
    • When a digit is typed into an input field, automatically move focus to the next empty input field.
    • When the Backspace key is pressed in an empty input field, automatically move focus to the previous input field and clear its content.
    • Pressing Backspace in a non-empty field should clear that field's digit but not move focus.
  4. Paste Functionality: Allow users to paste a full OTP string (e.g., "123456") into any of the input fields. The component should distribute the pasted digits across the subsequent fields.
  5. onChange Callback: The onChange prop should be called with the complete OTP string (e.g., "123456") whenever the full code is entered. If the code is incomplete, onChange should be called with an empty string "".
  6. Input Restrictions: Ensure only numeric digits (0-9) can be entered into the fields.

UI/UX expectations

  • Visual Representation: Each digit should be displayed within its own distinct visual input box (e.g., square or rounded rectangle).
  • Focus State: Clearly indicate the currently focused input field (e.g., with a distinct border or background color).
  • Accessibility: Implement appropriate ARIA attributes to improve accessibility for screen readers and ensure full keyboard navigability (Tab/Shift+Tab).
  • Placeholder: Empty input fields should display a subtle placeholder (e.g., _ or empty).

Acceptance criteria

  • The OTPInput component renders N individual input elements based on the length prop.
  • Typing a digit in field i automatically moves focus to field i+1.
  • Pressing Backspace in an empty field i moves focus to field i-1 and clears its content.
  • Pressing Backspace in a non-empty field i clears its content without moving focus.
  • Pasting a string of N digits into any field correctly populates all subsequent fields.
  • The onChange callback is invoked with the complete N-digit string when all fields are filled.
  • The onChange callback is invoked with "" when the OTP is incomplete.
  • Input fields only accept single numeric digits; non-numeric input is ignored.
  • The component is fully navigable using Tab/Shift+Tab keys.
  • Each input field has appropriate aria-label or aria-describedby attributes.

Constraints

Constraints & assumptions

  • The length prop will always be a positive integer between 4 and 8.
  • The OTP will consist solely of numeric digits (0-9).
  • You should use React and TypeScript.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed; build from primitive HTML elements.

Out of scope

  • Complex form validation beyond basic digit input (e.g., server-side validation).
  • Advanced animations or transitions.
  • Support for non-numeric OTPs or variable character types.
  • Styling for different themes or responsive layouts (focus on functionality and basic visual feedback).

What interviewers evaluate

  • Component Design & Reusability: How well the OTPInput component is structured, its API, and its ability to be reused.
  • State Management & Logic: Efficiency and correctness of state updates, handling complex input interactions (typing, backspace, paste).
  • Accessibility: Adherence to web accessibility standards for keyboard navigation and screen reader support.
  • Code Quality: Readability, maintainability, type safety (TypeScript), and adherence to best practices.

Loading editor...