Skip to content
Tutorials Center
My learning

Topics

Lesson 131 of 132 ยท Practice and Review

Machine-Coding Interview Problems in React

Prepare for React machine-coding interviews with a repeatable approach and worked solutions - autocomplete, accordion and tabs, pagination, nested checkboxes, infinite scroll and modal - with checklists.

A machine-coding round is an interview where you build a small working UI in 45 to 90 minutes: an autocomplete box, a star rating, an accordion. There is no puzzle to solve and no trick question. The interviewer watches how you structure state, name things, handle edge cases and keep the code readable while you build something that actually runs. Candidates fail these rounds not because they cannot write React, but because they start typing without a plan, forget the keyboard, or run out of time before anything works.

This lesson gives you a repeatable approach and four fully worked, runnable solutions, each followed by the checklist an interviewer would apply. Two more problems (infinite scroll and a modal) come with the approach and the pitfalls. They build on lifting state up, choosing state structure and accessibility.

A repeatable approach

  1. Clarify (2 minutes). Ask what is in scope: data source (static list or API?), keyboard support, accessibility, styling expectations, how many items. Write the answers down; they become your checklist.
  2. Plan the state (3 minutes). List every value that changes, and ask which are derived. Store the minimum (for example the selected id, not the selected object), and compute the rest during render. See choosing state structure.
  3. Sketch the component tree. One component per responsibility; pass data down and events up.
  4. Get the happy path working first, with hard-coded data, with no styling beyond layout. A working simple version beats a half-finished perfect one.
  5. Add edge cases: empty states, loading, errors, boundaries (first and last page), duplicate clicks.
  6. Add keyboard and accessibility: real buttons, labels, aria-* where needed, focus handling.
  7. Polish and explain. Name things clearly, remove dead code, and say aloud what you would do with more time (tests, virtualization, caching).

Think aloud. A silent candidate with perfect code scores lower than a talking candidate with one bug that they find and explain.

Problem 1: Autocomplete

Requirements: a text box; as the user types, show matching suggestions (case-insensitive); click or press Enter to select; arrow keys move through the list; Escape closes it; do not fire a search on every keystroke.

State plan: query (the text), open (is the list visible), activeIndex (highlighted option). Suggestions are derived from query, not stored. A debounced query simulates the API-call guard: filter on the debounced value, not the raw one.

Autocomplete with debounce and keyboard navigation

Checklist: debounce the filter, not the input (the box must stay responsive); handle "no matches"; clear activeIndex when the list changes; onMouseDown (not onClick) on options, because onBlur of the input fires first and would close the list before the click lands; close on Escape and blur; mention caching and request cancellation if the data comes from an API (async patterns); use aria-activedescendant so screen readers follow the highlight.

Problem 2: Accordion and tabs

Requirements (accordion): several sections; clicking a header opens it; optionally only one open at a time; the header is keyboard accessible. Tabs: a row of tabs and one visible panel; arrow keys move between tabs.

State plan: do not store a boolean per section. Store openId (single mode) or a set of ids (multiple mode). For tabs store activeId. A single value makes "only one open" true by construction.

Accordion (single or multiple) and tabs with arrow keys

Checklist: headers are <button>s inside headings; aria-expanded and aria-controls; a Set of open ids; the key on Accordion resets state when the mode changes; roving tabIndex and arrow keys for tabs (only the active tab is a tab stop); hidden rather than conditional rendering when panels should keep their state.

Problem 3: Pagination

Requirements: show 5 items per page from a list of 47; Previous and Next buttons; numbered page buttons; disable Previous on the first page and Next on the last; let the user change the page size.

State plan: only page and pageSize. The visible items, the page count and the range text are all derived. Reset to page 1 when the page size changes, otherwise you can end up beyond the last page.

Pagination with page size

Checklist: page count with Math.ceil; the last page may be short (47 items, 10 per page leaves 7); clamp the page (safePage) so shrinking the data never strands you; disable at the ends; aria-current="page"; a window of page numbers rather than hundreds of buttons; mention server-side pagination (pass page to the API) as the real-world version.

Problem 4: Nested checkboxes

Requirements: a tree of options (Fruits with Apple, Banana; Vegetables with Carrot, Pea); checking a parent checks all its children; unchecking a child updates the parent, which is indeterminate when only some children are checked.

State plan: store only the checked leaf ids. The parent's state (all, some, none) is derived from its children. Storing the parent's checkbox separately creates two sources of truth that fight each other.

Nested checkboxes with an indeterminate parent

Checklist: one Set of leaf ids; parent state derived; indeterminate is a DOM property, set through a ref in an effect; one handler that toggles many ids; for deeper trees, make the group recursive and compute each level from its descendants.

More problems to practice

Infinite scroll. Render a page of items; put a small sentinel element at the end of the list; use an IntersectionObserver in an effect to load the next page when the sentinel becomes visible; stop when the data runs out; show a loading row; clean up with observer.disconnect(). Pitfalls: loading twice (guard with a loading flag), forgetting the end state, and unbounded DOM growth (mention virtualization).

Modal dialog. Render into a portal; close on Escape, on the backdrop click and with a close button; move focus into the dialog on open and return it to the trigger on close; trap Tab inside; lock the page scroll; use role="dialog", aria-modal="true" and aria-labelledby. The native <dialog> element with showModal() gives much of this for free and is a strong answer.

Star rating, toggle, progress bar. See Reusable Widgets.

Other favorites: to-do list with filters (project), tic-tac-toe (project), stopwatch, debounced search (project), file explorer (recursive tree), table with sorting, countdown timer, form with validation (form validation).

What interviewers actually score

Area What good looks like
State design Minimal state, everything else derived; no duplicated truths
Component structure Small components, clear props, no giant function
Correctness Edge cases handled (empty, first, last, rapid clicks)
Accessibility Native elements, labels, keyboard support
Code clarity Descriptive names, no dead code, consistent style
Communication Explains trade-offs and what they would do next

Common mistakes

  • Starting with styling instead of behavior.
  • Storing derived data (a filtered list, a page count) in state.
  • Using array index as a key for lists that change.
  • Forgetting cleanup for timers, observers and listeners.
  • Ignoring the keyboard. Interviewers notice.
  • Spending 40 minutes on one feature and leaving the rest. Time-box and cut scope in a stated way.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a machine-coding round, what should you do first after reading the problem?

    Asking about scope and listing the minimum state prevents rework and shows structured thinking.
  2. 2. Why use onMouseDown instead of onClick for options in an autocomplete list?

    Blur happens between mousedown and click, so a list closed on blur would disappear before click. Handling mousedown (and preventing default) avoids that.
  3. 3. What is the best state for an accordion that allows only one section open?

    One value makes "only one open" true by construction. Per-section booleans must be kept in sync by hand.
  4. 4. How should a parent checkbox represent "some children checked"?

    The parent state is derived from the children. indeterminate has to be set on the DOM node, usually through a ref in an effect.
  5. 5. What happens to pagination if the page size changes while you are on page 9 and you do not reset the page?

    Fewer pages exist after increasing the size, so reset to page 1 or clamp the page to the new page count.

Summary

  • Follow a routine: clarify, plan minimal state, sketch components, get the happy path working, handle edge cases, add keyboard and accessibility, then polish.
  • Store the minimum (ids, page, query) and derive the rest during render.
  • Autocomplete: debounce, aria-activedescendant, onMouseDown for options. Accordion and tabs: one openId, roving tabIndex. Pagination: clamp the page. Nested checkboxes: a set of leaf ids, parent derived.
  • Practice infinite scroll and modal with IntersectionObserver and portals or <dialog>.
  • Think aloud and say what you would add with more time.

Next: React Interview Questions and Answers