Real-world interview practice

JavaScript and TypeScript Interview Scenarios: 8 Real-World Problems and How to Answer Them

Explain how you handle asynchronous work, uncertain data, browser performance, and safe user-facing applications.

Async behavior Type safety Browser performance Application security

How to approach scenario questions

Interviewers want to hear how you investigate constraints, make trade-offs, and verify a solution. Use each problem to explain a practical decision process, not just name a language feature.

A clear answer structure

  1. Clarify requirements and failure conditions.
  2. Explain the approach and why it fits.
  3. Cover trade-offs, safety, and edge cases.
  4. Describe how you would test and measure it.
Real-world problem 1

Search results appear in the wrong order when a user types quickly. How would you fix it?

The scenario

Several search requests are in flight at once. A slow response for an older query overwrites the latest results.

A strong answer

Cancel the previous request with AbortController when possible, and associate each request with a sequence ID so stale responses are ignored. Debounce input to reduce unnecessary calls, but keep cancellation and stale-response protection because debouncing alone does not solve ordering.

Real-world problem 2

Rendering a large response freezes the browser. What would you change?

The scenario

The API returns tens of thousands of records and the page becomes unresponsive while parsing and rendering them.

A strong answer

Measure whether parsing, transformation, layout, or rendering is the bottleneck. Render only visible rows with virtualization, paginate or stream data where possible, and move expensive CPU transformations to a Web Worker. Avoid creating large intermediate arrays when an incremental approach works.

Real-world problem 3

A TypeScript API client receives inconsistent JSON. How do you keep the application safe?

The scenario

A backend sometimes omits fields or returns values with unexpected types, although the frontend interface claims every field is valid.

A strong answer

Treat external JSON as unknown at the boundary, validate its shape at runtime, and convert it into a trusted application type only after validation. Model optional fields explicitly and handle invalid responses as a typed error instead of relying on a TypeScript assertion.

Real-world problem 4

A user double-clicks Submit and creates two orders. How would you prevent duplicates?

The scenario

The browser sends two nearly identical order requests before the first response returns.

A strong answer

Give each logical submission an idempotency key and enforce it on the server with a uniqueness constraint or durable request record. The UI can disable the submit control while waiting, but the backend must remain correct when requests are repeated or retried.

Real-world problem 5

A page accumulates event listeners after navigation. What would you inspect?

The scenario

Memory use grows as users open and close a panel, and events begin firing multiple times.

A strong answer

Check whether listeners, timers, subscriptions, or observers outlive the UI that created them. Remove them during component cleanup or use an AbortController signal for grouped listeners. Verify that cleanup runs on every unmount and test by repeatedly opening and closing the panel.

Real-world problem 6

Several independent requests power one dashboard, and one service may fail. How should the page load?

The scenario

Using Promise.all rejects the whole dashboard when one optional widget endpoint is unavailable.

A strong answer

Use Promise.all when every result is required for a valid page. Use Promise.allSettled when widgets can fail independently, then render successful results and a clear error state for each failed widget. Add timeouts and avoid silently presenting missing data as a successful response.

Real-world problem 7

Totals differ by a few cents between the browser and the server. What is likely happening?

The scenario

A checkout page calculates prices with floating-point numbers and occasionally displays a different total from the payment service.

A strong answer

Binary floating-point cannot represent many decimal fractions exactly. Represent monetary values as integer minor units, such as cents, or use a decimal arithmetic library with an explicit rounding policy. Keep the same currency precision and rounding rules on client and server.

Real-world problem 8

A user-controlled string is rendered as markup. How would you reduce XSS risk?

The scenario

A profile field is inserted into the page using innerHTML and malicious input can execute in another user session.

A strong answer

Render plain text with textContent or a framework’s escaped text binding. If rich HTML is a genuine requirement, sanitize it with a maintained allowlist sanitizer and apply context-appropriate output handling. Do not treat client-side validation as a security boundary.