Skip to the solution
MediumMachine Coding11 min read

Build a Search Bar with History

1.1k views

The problem

Question presented to candidate: "Add recent searches to our article search. When someone submits a query, remember it. When they click into the box, show their last five searches; picking one runs it again. The list should survive a page reload, should not contain duplicates, and should be usable from the keyboard."

Requirements:

  • Submitting (Enter or the Search button) runs the search and records the term: trimmed, newest first, case-insensitive de-duplication (a repeat moves to the top), capped at 5.
  • Persist the list in localStorage; survive corrupt or missing data and storage that throws.
  • Clicking the input opens a "Recent searches" dropdown, filtered by what is typed; each row re-runs its search and has a remove (ร—) button; there is a "Clear all".
  • Keyboard: Down arrow moves into the list, Up / Down move between rows, Delete removes the focused row, Escape closes and returns to the input.
  • Close on a press outside or when focus leaves the widget; follow changes made in another tab.

What a strong answer should cover:

  • Pure helper functions (addToHistory, parseHistory) separate from the UI, so dedupe and cap logic are testable on their own.
  • Defensive localStorage access: try/catch around reads and writes, validating the parsed JSON shape.
  • One commit() path that updates state and storage together, instead of an effect that writes storage on every render.
  • Focus management: where focus goes after a row is removed, and why real focus moving between buttons is used here instead of aria-activedescendant.
  • Outside-press and focus-leave closing, and the storage event for cross-tab sync.

Clarifying questions expected:

  • "Is history per device (localStorage) or per account (server)? Should it sync across tabs?"
  • "How many entries, and should 'React' and 'react' count as the same search?"
  • "Should the dropdown open on focus, on click, or only when the user presses Down?"

Code / implementation expected: Yes. A working component with persisted, de-duplicated, capped history and keyboard support.

  • React
  • Search
  • Storage

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read ยท 9 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import HistorySearch from "./src/components/HistorySearch";

export default function App() {
  return (
    <main className="page">
      <h1>Article search</h1>
      <HistorySearch />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 31 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build Wordle

Continue