Back

Accessible React Tabs

easy

Streak

0 days

Progress

0%

Submitted

0

Accessible React Tabs

React40 mineasyFree

Prompt

Complete the reusable Tabs({ tabs, defaultActiveIndex = 0 }) component in 40 minutes. Each item has a unique id, label, and React content. Mouse selection is supplied; finish focus management and automatic keyboard activation.

  • ArrowLeft/ArrowRight move focus and selection together, wrapping at both ends. Home/End select the first/last tab.
  • Only the active tab is in the Tab sequence. Tab then enters the active text panel; do not intercept Tab.
  • Use tablist, tab, and tabpanel roles with matching ARIA relationships. Keep inactive panels hidden.
  • Empty input renders nothing. An invalid initial index falls back to zero. Two instances must have distinct DOM IDs.

Use the supplied HTML, CSS, JavaScript, and React fixture. Tabs are static during the core exercise; dynamic reordering, disabled tabs, remote content, and controlled state are follow-ups.

Learn and continue

Prerequisite: Semantic HTML and accessibility. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements

  • →ArrowLeft on HTML selects and focuses React; ArrowRight wraps back to HTML
  • →Home and End select and focus boundary tabs without scrolling
  • →Exactly one tab has tabIndex=0; Tab reaches the active text panel
  • →Each aria-controls and aria-labelledby resolves to an existing element
  • →Empty input returns nothing; invalid initial indices fall back to the first tab
  • →Two component instances have disjoint DOM IDs

Starter packet

What the interviewer handed you

Complete the supplied tabs component using the prompt contract. The shell and fixtures are provided; TODOs identify the behavior to implement.

Manual acceptance checks

  • ArrowLeft on HTML selects and focuses React; ArrowRight wraps back to HTML
  • Home and End select and focus boundary tabs without scrolling
  • Exactly one tab has tabIndex=0; Tab reaches the active text panel
  • Each aria-controls and aria-labelledby resolves to an existing element
  • Empty input returns nothing; invalid initial indices fall back to the first tab
  • Two component instances have disjoint DOM IDs

Likely follow-ups

  • • Would you automatically activate on focus?
  • • How would disabled tabs affect keyboard navigation?
Example
Loading preview...
For the best coding experience, we recommend using a desktop device.
Preparing Sandbox...
Premium interview report

What interviewers score in this build

Use this before reading the code. It tells you what to say, what to test, and where machine-coding candidates usually lose points.

Interview signals

  • Core behavior: ArrowLeft on HTML selects and focuses React; ArrowRight wraps back to HTML
  • State and identity: Selection, keyboard focus, visible panel, and ARIA state must agree.
  • Accessibility: Use the documented native keyboard behavior, meaningful names, and visible focus.
  • Edge cases: Demonstrate: Empty array, One tab, Invalid defaultActiveIndex, Two instances with identical item IDs, Native Tab and visible focus.

Time checkpoints

  1. 1

    0:00: Read the contract and walk through the fixture.

  2. 2

    5:00: Define state, identity, and keyboard behavior.

  3. 3

    30:00: Finish core interactions and lifecycle cleanup.

  4. 4

    35:00: Run the acceptance checks and edge cases.

Edge-case checklist

Empty array
One tab
Invalid defaultActiveIndex
Two instances with identical item IDs
Native Tab and visible focus

Common mistakes

  • Updating selection without moving focus
  • Duplicate DOM IDs across instances
  • Removing focus outlines
  • Intercepting Tab or unrelated arrow keys
SolutionRead-only · Live Preview

Technical Explanation

Start with the interaction model

This exercise uses automatic activation because all panel content is already available. Moving focus to another tab immediately selects it. For panels that require a slow request, discuss manual activation with Enter or Space instead.

Store one selection

Keep an index in state and derive the visible panel from it. Validate defaultActiveIndex once when initializing state; a negative, fractional, or out-of-range value falls back to zero. The default is an initial value, not a controlled prop.

Move focus as well as selection

ArrowRight uses (index + 1) % length; ArrowLeft uses (index - 1 + length) % length. Home and End choose the boundaries. Prevent the default only for those handled keys, update selection, and focus the matching button using refs. Tab remains native.

Keep relationships valid

A useId prefix separates component instances. Each tab controls an existing panel; each panel is labelled by its tab. Render all panels with hidden on inactive ones, which preserves their local state and removes them from normal focus and accessibility navigation. Rendering only the active panel is another valid design, but unmounts its state.

Make focus visible

Exactly one tab has tabIndex=0. The text panel is also focusable. Keep a visible focus outline on both. Styling aria-selected is valid and keeps the visual selection tied to the semantic state.

Walk through it

Focus HTML, press Left to reach React, then Home to return to HTML. Tab enters the HTML panel. Render a second Tabs instance with the same item IDs and verify its DOM IDs differ. Test empty input and defaultActiveIndex=99.

Interview Criteria

Core behavior

ArrowLeft on HTML selects and focuses React; ArrowRight wraps back to HTML

State and identity

Selection, keyboard focus, visible panel, and ARIA state must agree.

Accessibility

Use the documented native keyboard behavior, meaningful names, and visible focus.

Edge cases

Demonstrate: Empty array, One tab, Invalid defaultActiveIndex, Two instances with identical item IDs, Native Tab and visible focus.

Reasoning

Explain the state model and the tradeoff behind the interaction contract.

Time Checkpoints

0:00

0:00: Read the contract and walk through the fixture.

5:00

5:00: Define state, identity, and keyboard behavior.

30:00

30:00: Finish core interactions and lifecycle cleanup.

35:00

35:00: Run the acceptance checks and edge cases.

40:00

40:00: Explain tradeoffs and follow-up scope.

Streak

0 days

Last active: Sign in to track

Progress

0%

0/0 solved

Submitted

0

Solutions pushed to review history.

More Machine Coding

View all Machine Coding →
Machine CodingMedium

React Tic-Tac-Toe Engine

Machine CodingMedium

Performant Image Slider

Machine CodingMedium

API Search and Render Image

Machine CodingMedium

Recursive File Explorer

Machine CodingEasy

Stopwatch

Machine CodingEasy

Tooltip

Related hubs

  • All machine coding
  • Frontend machine coding guide
  • React machine coding questions
  • UI coding interview questions
  • React UI interview questions
  • Airbnb interview
  • Meta interview

FAQ

What makes Accessible Tabs a strong interview question?

It checks ARIA roles, keyboard left/right navigation, selected state, and separation of trigger vs panel content.

Common mistakes candidates make?

Missing keyboard support, wrong ARIA attributes, remounting panels unnecessarily, or mixing uncontrolled selection with parent state.

Related practice?

Accordion, modal system, and multi-step form.