Home
← All questions

Nested checkboxes with parent/child sync | Frontend Machine Coding

MediumPremiumMachine Coding

Frontend Machine Coding interview practice: Build a nested checkbox tree where parent and child selection states stay in sync.

Problem

Render a tree of checkboxes with parent/child relationships.

Requirements

  • Checking a parent checks all descendants; unchecking unchecks all descendants.
  • When all children are checked, the parent becomes checked; if some (not all) are checked, the parent shows an indeterminate state.
  • Works for arbitrary nesting depth.

Hints / Approach

  • Model: { id, label, children: [] }. Keep a checked map keyed by id, or store checked on nodes immutably.
  • Downward propagation: on toggling a node, set the same value on the whole subtree (DFS).
  • Upward propagation: after a change, recompute each ancestor — allChecked → checked, someChecked → indeterminate, noneChecked → unchecked. Set inputEl.indeterminate = true (it's a DOM property, not an attribute/state).
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium