Interview Q&A · JavaScript · 8 questions in this topic

Scenario-Based Questions — JavaScript Interview Q&A

All JavaScript topics

Judgment questions — slow pages, broken fetches, and state bugs from real on-call rotations. Part of Scenarios & Rapid Fire · 204 questions in the JavaScript bank.

A page is slow to interact. How do you diagnose it?

Answer

Profile first (Performance panel: long tasks, JS vs render vs network), check bundle bytes and third-parties, look for hydration/render storms and unmemoized lists, then fix the measured bottleneck — split code, virtualize lists, debounce handlers, move work to workers.

Measure-first beats guess-memoizing every time.

Key points

  1. 1Profile — long tasks, flame chart.
  2. 2Bytes — bundle, vendors, images.
  3. 3Render — memoize, virtualize, composite-only.

💡 INP-plus-bundle is the first-two-checks pair.

Fetches fail intermittently in production. How do you harden them?

Answer

Timeouts (AbortController race), retries with backoff+jitter on transient failures, idempotency keys for unsafe methods, offline/empty/loading states in UI, and request logging with correlation ids. Never infinite-retry non-idempotent writes.

Timeout-plus-backoff-plus-states is the resilience triple.

Key points

  1. 1Bound — timeouts, attempt caps.
  2. 2Retry — transient-only, jittered backoff.
  3. 3Surface — loading/error/offline UI.

State updates do not re-render (stale UI). How do you debug it?

Answer

Check reference identity (mutated-in-place state is invisible to shallow compare), key stability in lists (index keys reshuffle state), and closure staleness (handlers capturing old state — functional setState or refs fix it).

Mutation-plus-stale-closure covers most stale-UI incidents.

Key points

  1. 1Identity — new references required.
  2. 2Keys — stable ids, not indices.
  3. 3Closures — functional updates, refs.

Example

Example.jsjs
// setList((prev) => [...prev, item]); // fresh ref + fresh closure

💡 Functional-updates fix both mutation and staleness classes.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits