Frontend School

Secure OTP Verification UI

Machine Coding
medium

Secure OTP Verification UI

Medium
React
Frontend
UI
OTP
Accessibility
Machine Coding

Overview

At Zepto, ensuring a seamless and secure user experience for critical actions like login, payment, or account recovery is paramount. OTP (One-Time Password) verification is a key part of this process. A well-designed OTP input component not only enhances security but also significantly improves user satisfaction by making the input process intuitive and error-free. This problem challenges you to build a robust and user-friendly OTP input component that handles various interaction patterns and states.

What you need to do / Task

Your task is to create a reusable OtpInput React component with the following features:

  1. Component Structure:
    • Accept a length prop (e.g., 6 for a 6-digit OTP).
    • Render length individual input fields for each digit.
  2. Focus Management:
    • Auto-advance: When a digit is typed into an input field, focus should automatically shift to the next empty input field.
    • Backspace Navigation: When Backspace is pressed in an input field, it should clear the current field's digit and move focus to the previous input field. If already in the first field, it should only clear the digit.
    • Arrow Key Navigation: Support left/right arrow key navigation between input fields.
  3. Paste Functionality:
    • When a user pastes a string (e.g., "123456") into any of the input fields, the component should automatically parse the string and distribute the digits across the subsequent input fields. Non-numeric characters should be ignored.
  4. Callbacks:
    • Expose an onChange prop that is called with the current OTP string whenever any digit changes.
    • Expose an onComplete prop that is called with the full OTP string only when all length digits have been successfully entered.
  5. State Handling:
    • Accept an isError boolean prop. When true, the input fields should visually indicate an error state (e.g., red border).
    • Accept an isLoading boolean prop. When true, all input fields should be disabled, and a visual loading indicator should be displayed.
  6. Accessibility:
    • Ensure the component is accessible, supporting keyboard navigation and proper semantic HTML attributes.

UI/UX expectations

  • Layout: Display input fields horizontally with a small gap between them.
  • Visual Cues:
    • Clear focus indication (e.g., a distinct border color on focus).
    • Error state should be visually prominent (e.g., red border around fields).
    • Loading state should disable inputs and show a subtle loading spinner or text.
  • Input Behavior: Input fields should primarily accept numeric characters.
  • Responsiveness: The component should adapt gracefully to different screen sizes, though a simple desktop-first approach is acceptable for this problem.

Acceptance criteria

  • The component renders length distinct input fields.
  • Typing a digit in an input automatically moves focus to the next input.
  • Pressing Backspace clears the current digit and moves focus to the previous input (unless it's the first).
  • Pressing Left Arrow / Right Arrow moves focus to the adjacent input.
  • Pasting a valid OTP string (e.g., "123456") into the first input populates all fields correctly.
  • The onChange callback fires with the current OTP string after each digit entry/change.
  • The onComplete callback fires with the full OTP string only when all digits are entered.
  • Inputs are disabled and a loading indicator is visible when isLoading is true.
  • Input fields display an error style when isError is true.
  • The component is navigable and usable via keyboard alone.

Constraints

Constraints & assumptions

  • Build the component using React.js.
  • OTP digits will always be numeric (0-9).
  • The length prop will be an integer between 4 and 8, inclusive.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed for the core OTP input fields themselves. You are free to use basic CSS for styling.
  • Assume a modern browser environment.

Out of scope

  • Backend API integration for OTP validation.
  • Complex animations beyond basic loading indicators.
  • Full form submission logic for the parent component.
  • Advanced internationalization (i18n) or right-to-left (RTL) layout support.

What interviewers evaluate

  • Component Design & State Management: Clarity, correctness, and efficiency of React component structure, state, and effect management (e.g., useState, useRef, useEffect).
  • Event Handling & DOM Manipulation: Robust and accurate implementation of keyboard events, paste events, and programmatic focus management.
  • Accessibility & User Experience: Thoughtfulness in making the component accessible (semantic HTML, keyboard navigation) and intuitive for the end-user.
  • Code Quality & Best Practices: Readability, maintainability, adherence to React best practices, and effective use of TypeScript (if chosen).

Loading editor...