Skip to the solution
MediumMachine Coding10 min read

Build a Custom Multi-select Dropdown

425 views

The problem

Question presented to candidate: "Our profile form needs a 'Skills' picker. Users open a dropdown, filter by typing, and pick as many skills as they like; picked skills appear as chips in the field and each chip can be removed. It must work with the keyboard and close when you click away."

Requirements:

  • A field showing the selected values as chips (each with a remove button) followed by a search input; the dropdown lists options with a checkmark for selected ones.
  • Typing filters options; Up / Down move the highlight; Enter toggles the highlighted option and the dropdown stays open for more picks; Escape closes.
  • Backspace in an empty search removes the last chip; removing a chip returns focus to the input.
  • "Select all shown" adds every currently visible option (disabled when they are all selected already); "Clear all" empties the selection.
  • The emitted value is an array of option ids in the options order, whatever order they were clicked in; close on an outside press or when focus leaves; announce changes politely.

What a strong answer should cover:

  • Controlled value (array of ids) with a Set derived for O(1) membership checks while rendering.
  • Normalising the value to the options order so the output is stable.
  • A listbox with aria-multiselectable="true" where aria-selected means chosen (unlike a single-select popup, where it marks the highlighted option), and aria-activedescendant for the highlight.
  • Focus management for chip removal and Backspace, and mousedown preventDefault on options.
  • Outside-press and focus-leave closing, and a live region for "React selected. 3 selected."

Clarifying questions expected:

  • "Is there a maximum number of selections?"
  • "Should the selection order matter, or is the options order fine?"
  • "Do 'Select all' and 'Clear' apply to the filtered options or to everything?"

Code / implementation expected: Yes. A reusable, controlled multi-select component with chips, filtering and keyboard support.

  • React
  • Dropdown
  • Accessibility

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

10 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 { useState } from "react";
import MultiSelect from "./src/components/MultiSelect";
import { SKILLS } from "./src/utils/skills";

export default function App() {
  const [skills, setSkills] = useState([]);
  return (
    <main className="page">
      <h1>Your skills</h1>
      <MultiSelect
        label="Skills"
        options={SKILLS}
        value={skills}
        onChange={setSkills}
      />
      <p className="value" data-testid="value">
        Value: {JSON.stringify(skills)}
      </p>
    </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 57 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 a Signup Form with async username availability check

Continue