Build an N-box OTP input with auto-advance, backspace and paste.
Skip to solutionKEEP THE
mediumMachine Coding
Build an OTP Input
119 views
01
Understand the problem
ReactPasteBackspace
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Solve in
An OTP (one-time-password) input is the row of single-character boxes you fill after a 2FA / SMS code is sent. It looks trivial, but a good implementation forces you to handle focus management, keyboard events, paste, and controlled inputs all at once.
The mental model (the part that matters)
The single
Solution ready — 2 min read
Classified // press E to declassify
04
React solution
Complete solutionRun Playground
import { useState } from 'react';
import OtpInput from './src/OtpInput';
export default function App() {
const [code, setCode] = useState('');
const [done, setDone] = useState(false);
return (
<div style={{ fontFamily: 'system-ui, sans-serif', padding: 32, textAlign: 'center' }}>
<h2 style={{ marginBottom: 6 }}>Verify your number</h2>
<p style={{ color: '#666', marginTop: 0, marginBottom: 20 }}>
Enter the 6-digit code. Try typing, pasting "123456", and backspace.
</p>
<div style={{ display: 'inline-block' }}>
<OtpInput length={6}
onChange={(v) => { setCode(v); setDone(false); }}
onComplete={(v) => { setCode(v); setDone(true); }} />
</div>
<p style={{ marginTop: 22, fontFamily: 'monospace', fontSize: 16 }}>
value: {code || '(empty)'} {done ? 'OK' : ''}
</p>
</div>
);
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 59 of 87 decoded in the Frontend Machine Coding track. One more won't hurt.