Autocomplete (Offline)
Difficulty: Advanced
Problem Statementβ
Filter local suggestions list as user types.
Live Demoβ
Try the running app below β this is the target behavior for the challenge.
Autocomplete (Offline) β Live Demo
What Interviewers Expectβ
- Controlled input.
- Case-insensitive filtering.
- Empty state when no matches.
Step-by-Step Implementationβ
Step 1 β Controlled search termβ
Bind input value to state.
const [term, setTerm] = useState('');
Step 2 β Derived resultsβ
Use useMemo to filter suggestions.
return COUNTRIES.filter((c) => c.toLowerCase().includes(q));
Complete Implementationβ
Copy-paste ready single-file solution. Use this as your reference after attempting the challenge yourself, or paste it into CodeSandbox / StackBlitz to run locally.
App.jsx
import React, {useMemo, useState} from 'react';
const COUNTRIES = ['India', 'Indonesia', 'Ireland', 'Israel', 'Italy', 'Japan', 'Jordan'];
export default function App() {
const [term, setTerm] = useState('');
const results = useMemo(() => {
if (!term.trim()) return [];
const query = term.toLowerCase();
return COUNTRIES.filter((country) => country.toLowerCase().includes(query));
}, [term]);
return (
<div>
<input
value={term}
onChange={(e) => setTerm(e.target.value)}
placeholder="Search country"
/>
<ul>
{results.map((country) => (
<li key={country}>{country}</li>
))}
</ul>
</div>
);
}
Final Checklistβ
- UI works for primary flow
- Edge cases handled (empty/disabled/loading where relevant)
- State updates are immutable
- Components are readable and reasonably split
- You can explain trade-offs out loud in an interview
Next Challengeβ
Continue to the next item in the sidebar when you're comfortable implementing this from scratch in 30β45 minutes.