Question presented to candidate: "Here is an array of employee records. Render it as a table where clicking a column header sorts by that column, ascending, then descending, then back to the original order. Add a search box and a role dropdown to filter it, and show five rows per page. The columns have different types: text, numbers, dates and a yes or no flag."
Requirements:
- Columns described by config (
key,label,type), not hard-coded markup. - Header click cycles none, ascending, descending; a new column starts ascending. Only one column sorts at a time.
- Correct comparison per type: locale-aware text, numeric numbers, real dates, booleans; empty values always last.
- Stable sort: rows that tie keep their original relative order.
- A search box (name or role, case-insensitive, trimmed) and a role select, combined with AND.
- Five rows per page with Prev / Next and "Page x of y"; any filter or sort change returns to page 1; an empty-state row when nothing matches.
- Accessible headers: real buttons inside
th,aria-sorton the sorted column, a caption that states the count and the sort.
What a strong answer should cover:
- A derived pipeline: source rows, then filter, then sort, then slice. Only the inputs (query, role, sort, page) are state; every stage is computed and memoised.
- Why
sort()on the source array is a bug (it mutates props or state), and copying first. - Comparators by type,
Intl.Collatorfor text, and an index tiebreaker so the result is stable and deterministic. - Clamping or resetting the page when the row count changes.
aria-sort,scope="col", buttons for keyboard access, and horizontal scrolling on narrow screens.
Clarifying questions expected:
- "Is the data small enough to sort in the browser, or should sorting and filtering go to the server?"
- "Do you want multi-column sort (shift-click) or one column at a time?"
- "How should empty values sort?"
Code / implementation expected: Yes. A reusable table component driven by a column config, in React, Vue or Angular, with the sorting and filtering logic in plain functions.