How to Build an OTP Input Component in an Interview
The OTP input is a common machine coding question. Here is how to build it in 90 minutes.
How to Build an OTP Input Component in an Interview
The OTP input is one of the most common machine coding questions. Here is how to build it in 90 minutes.
Requirements
- 4-6 input boxes for OTP digits.
- Auto-focus to the next box when a digit is entered.
- Backspace to go to the previous box.
- Only numeric input.
- Paste support (paste a full OTP).
- Submit when all boxes are filled.
Plan (5-10 minutes)
- HTML: 4-6
<input>elements withmaxlength="1". - State: an array of values.
- Events: input (auto-focus next), keydown (backspace), paste.
- UI: centered, spaced boxes, focused state styling.
Build Core (50 minutes)
HTML
<div class="otp-container"> <input type="text" maxlength="1" class="otp-input" data-index="0" /> <input type="text" maxlength="1" class="otp-input" data-index="1" /> <input type="text" maxlength="1" class="otp-input" data-index="2" /> <input type="text" maxlength="1" class="otp-input" data-index="3" /> </div> <button id="submit">Verify OTP</button>
JavaScript (Core)
const inputs = document.querySelectorAll(".otp-input"); inputs.forEach((input, index) => { input.addEventListener("input", (e) => { const value = e.target.value.replace(/\D/g, ""); // numeric only e.target.value = value; if (value && index < inputs.length - 1) { inputs[index + 1].focus(); // auto-focus next } }); input.addEventListener("keydown", (e) => { if (e.key === "Backspace" && !e.target.value && index > 0) { inputs[index - 1].focus(); // backspace to previous } }); });
Handle Edge Cases (20 minutes)
Paste Support
inputs.forEach((input, index) => { input.addEventListener("paste", (e) => { e.preventDefault(); const pasted = e.clipboardData.getData("text").replace(/\D/g, ""); pasted.split("").forEach((char, i) => { if (index + i < inputs.length) { inputs[index + i].value = char; } }); const nextIndex = Math.min(index + pasted.length, inputs.length - 1); inputs[nextIndex].focus(); }); }); `` #### Submit When Filled ```js function checkComplete() { const otp = Array.from(inputs).map((i) => i.value).join(""); if (otp.length === inputs.length) { document.getElementById("submit").disabled = false; } } `` ### CSS (10 minutes) ```css .otp-container { display: flex; gap: 12px; justify-content: center; } .otp-input { width: 48px; height: 56px; font-size: 24px; text-align: center; border: 2px solid #ccc; border-radius: 8px; } .otp-input:focus { border-color: #007bff; outline: none; } `` ### The Takeaway Build the OTP input: HTML (4-6 inputs with maxlength=1), JavaScript (auto-focus next, backspace to previous, numeric only, paste support), CSS (flexbox, centered, focus styling). Handle edge cases (paste, submit when filled). This is a common machine coding question that tests DOM manipulation and event handling.
Create 4-6 input elements with maxlength=1. Add event listeners for input (auto-focus next, numeric only), keydown (backspace to previous), and paste (split pasted text across inputs). Add CSS for spacing and focus styling. Handle submit when all inputs are filled.
Auto-focus to the next box when a digit is entered, backspace to go to the previous box, only numeric input, paste support (paste a full OTP across boxes), and submit/auto-verify when all boxes are filled.
Listen for the paste event, prevent default, get the pasted text from clipboardData, filter to numeric only, and split each character across the input boxes starting from the current index. Focus the last filled input.
Listen for keydown. If the key is Backspace and the current input is empty, focus the previous input. This lets the user go back to correct a digit.
Paste support (paste a full OTP), backspace to previous, only numeric input, submit when all filled, and focusing the correct input after paste. Also handle the case where the user types in the middle of the OTP.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

