Question presented to candidate: "We have a contacts screen that has to show 10,000 rows, and right now it renders every one of them and the page crawls. Build a list component that stays smooth by only putting the rows you can actually see into the DOM. Rows are a fixed 40px tall and the list box is 400px high."
Requirements:
- Render 10,000 rows in a 400px scrollable box with a scrollbar that reflects the full list height.
- Mount only the visible rows plus a small overscan (3 rows above and below), and keep that number constant while scrolling.
- Show a status line such as "Showing 501-510 of 10,000 ยท 16 rows in the DOM".
- Clamp correctly at the top and the bottom of the list; the last row must be reachable.
- Keep the list keyboard-scrollable and give screen readers the real list size.
What a strong answer should cover:
- The windowing maths:
first = floor(scrollTop / rowHeight),last = floor((scrollTop + viewportHeight - 1) / rowHeight), then widen by the overscan and clamp to[0, count - 1]. - A spacer element whose height is
count * rowHeight, with rows absolutely positioned atindex * rowHeight, so the native scrollbar stays honest. - What is state (only the scroll position or the derived range) versus what is derived (the slice of rows), and re-rendering only when the range actually changes.
- Overscan as a trade-off between blank flashes during fast scrolls and extra DOM work.
- Stable keys (item id, not loop position) and ARIA:
role="list",aria-setsize,aria-posinset, a focusable scroll container. - Limits: fixed versus variable row heights, browser maximum element height, find-in-page not seeing unmounted rows.
Clarifying questions expected:
- "Are row heights fixed, or can they vary with content?"
- "Does the list scroll inside a box or with the whole page?"
- "Do we need selection, keyboard navigation between rows, or jump-to-row?"
Code / implementation expected: Yes. A working list component with the scroll handler, the range calculation, a spacer and absolutely positioned rows, ideally with the windowing maths in a small pure function.