Back

Recursive File Explorer

medium

Streak

0 days

Progress

0%

Submitted

0

Recursive File Explorer

React35 minmediumFree

Prompt

Build FileExplorer({ root }) in 35 minutes from a nested file-system object. Each node has a globally unique id, a name, and type file or folder; folders may contain a children array.

  • Open the root initially. Other folders start collapsed and toggle independently.
  • Selecting a file highlights only that file. Folder clicks change expansion, not file selection.
  • Preserve descendant expansion and the selected file when an ancestor collapses and reopens.
  • Show an empty-folder message, tolerate missing children, and show an empty state for no root.
  • Render nested lists with folder/file buttons. Tab reaches visible buttons; Enter/Space activate them.

Use the supplied deep fixture, empty folder, and duplicate filenames. Stable IDs distinguish identical names. This is a nested disclosure navigator, not an ARIA tree widget; custom arrow-key traversal, editing, lazy loading, and virtualization are follow-ups.

Learn and continue

Prerequisite: Recursion. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements

  • →Root starts expanded; other folders start collapsed
  • →Open src/components/forms, select Input.jsx, collapse src, then reopen: nested expansion and selection survive
  • →Opening docs does not collapse src
  • →Two README.md files can be selected independently by ID
  • →An empty or missing children array shows Empty folder
  • →No root shows No files available
  • →Tab reaches only visible buttons; Enter/Space activate folder and file buttons

Starter packet

What the interviewer handed you

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

Manual acceptance checks

  • Root starts expanded; other folders start collapsed
  • Open src/components/forms, select Input.jsx, collapse src, then reopen: nested expansion and selection survive
  • Opening docs does not collapse src
  • Two README.md files can be selected independently by ID
  • An empty or missing children array shows Empty folder
  • No root shows No files available
  • Tab reaches only visible buttons; Enter/Space activate folder and file buttons

Likely follow-ups

  • • When would an object map beat a Set?
  • • How would you virtualize a ten-thousand-node tree?
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: Root starts expanded; other folders start collapsed
  • State and identity: Stable IDs identify nodes; a root-owned expansion Set survives collapsed branches.
  • Accessibility: Use the documented native keyboard behavior, meaningful names, and visible focus.
  • Edge cases: Demonstrate: No root, Empty folder, Missing children array, Deep fixture, Duplicate filenames, Collapse selected descendant then reopen.

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

    25:00: Finish core interactions and lifecycle cleanup.

  4. 4

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

Edge-case checklist

No root
Empty folder
Missing children array
Deep fixture
Duplicate filenames
Collapse selected descendant then reopen

Common mistakes

  • Using filenames or indices as identity
  • Mutating the expansion Set
  • Unmounting local folder state and unexpectedly losing expansion
  • Using role=tree without tree keyboard behavior
SolutionRead-only · Live Preview

Technical Explanation

Define keyboard behavior before markup

This exercise uses nested lists and native buttons. A folder button exposes aria-expanded; a file button exposes its selected toggle state with aria-pressed. Tab, Enter, and Space work through browser behavior. Do not add role=tree unless you also implement its distinct focus and arrow-key contract.

Recurse over data

A FileNode renders a row, then maps folder children to more FileNode elements. The same component handles each supplied depth without hard-coded levels. This is appropriate for an interview-sized fixture; it is not a promise of unlimited stack depth or production-scale rendering.

Keep durable UI state above hidden branches

FileExplorer owns expandedIds as a Set and selectedId as a scalar. If each nested component owned its open flag, unmounting children on parent collapse would erase that flag. A shared Set retains expansion independently of whether descendants are mounted.

Update immutably

Copy the previous Set, add or remove one ID, and return the copy from a functional state update. Mutating the old Set and returning it can prevent React from noticing the change. Use node.id as each key, since README.md can exist in several folders.

Derive structure and selection

Indent through nested list padding. File nodes do not recurse, even if malformed data supplies children. Missing children on a folder are treated as empty. Selecting a file changes only selectedId; toggling folders changes only expandedIds. When the selected file is hidden, retain its selection so it reappears after reopening.

Try the difficult path

Open src, components, then forms. Select Input.jsx. Collapse and reopen src: forms is still expanded and Input.jsx is still selected. Open assets to see Empty folder. Select the two README.md files separately to confirm name collisions do not share identity.

Interview Criteria

Core behavior

Root starts expanded; other folders start collapsed

State and identity

Stable IDs identify nodes; a root-owned expansion Set survives collapsed branches.

Accessibility

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

Edge cases

Demonstrate: No root, Empty folder, Missing children array, Deep fixture, Duplicate filenames, Collapse selected descendant then reopen.

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.

25:00

25:00: Finish core interactions and lifecycle cleanup.

30:00

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

35:00

35: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 CodingEasy

Accessible React Tabs

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
  • Google interview
  • Meta interview