Question presented to candidate: "What is the Virtual DOM, and why does React use one?"
What a strong answer should cover:
- A lightweight in-memory representation of the UI, made of plain objects (React elements). It is not a browser feature and not a copy of the DOM.
- On each render React builds a new tree, diffs it against the previous one (reconciliation), and applies only the differences to the real DOM.
- Why: real DOM operations are expensive relative to object comparison, and touching it triggers layout and paint.
- The honest framing: it is not faster than optimal hand-written DOM code. Diffing is extra work vanilla code skips. What it buys is making the declarative model fast enough to be practical.
- The diffing heuristics that make it O(n): different element types mean discard and rebuild; keys identify children across renders.
- Consequence of the type heuristic: changing an element's type unmounts the subtree and destroys its state.
- Fiber is the implementation that made this interruptible; the Virtual DOM is the data model, Fiber is the scheduler.
- Related but distinct: the shadow DOM is a browser encapsulation feature with no connection to this.
Clarifying questions expected:
- "Do you want the concept, or the diffing heuristics?"
Code / implementation expected: Optional. Showing that an element is a plain object makes the point faster than prose.