Question presented to candidate: "Power users want to drive our dashboard from the keyboard. Build a command palette like the ones in Linear or VS Code: Ctrl+K or Cmd+K opens it, you type a few letters such as 'gs' and it finds 'Go to Settings', arrow keys move, Enter runs the command, Escape closes. Write the fuzzy matching yourself instead of pulling in cmdk or kbar."
Requirements:
- Global hotkeys: Ctrl+K / Cmd+K toggles the palette;
/opens it unless the user is typing in a field. - A modal overlay with a search input and results grouped (Navigation, Actions, Help), plus a Recent group when the query is empty.
- A hand-written fuzzy matcher: query letters must appear in order; word starts and consecutive letters rank higher; matched letters are highlighted; keywords such as "theme" also find "Toggle dark mode".
- Keyboard: ArrowUp / ArrowDown (wrapping), Enter runs, Escape closes, Tab stays inside; mouse hover and click work too.
- Accessibility:
role="dialog"witharia-modal, a combobox input witharia-activedescendant, a listbox of options, focus returned to the previously focused element on close.
What a strong answer should cover:
- A command registry as plain data (
id,label,group,keywords,shortcut,perform) separate from the UI. - The matcher: subsequence check, a scoring scheme that can be explained, and how highlighting indices come out of it.
- Why focus stays in the input and the active option is announced with
aria-activedescendantinstead of moving focus. - Hotkey hygiene:
preventDefaultfor Ctrl+K, ignoring/inside inputs, cleaning up the global listener. - Trade-offs versus a library (cmdk, kbar) and what a production matcher adds.
Clarifying questions expected:
- "How many commands: tens or thousands? Do results come from a server?"
- "Should Ctrl+K inside the palette close it, or be ignored?"
- "Do commands have sub-pages (for example Change theme, then pick one)?"
Code / implementation expected: Yes. A fuzzy scoring function, a palette component with keyboard handling, and a host app with a few commands that visibly do something.