Interview Q&A / Node.js

INTERVIEW Q&A · 68 QUESTIONS

Node.js interview questions

The event loop on the server, streams, modules and building APIs with Express.

  1. If Node.js is single-threaded, how does it handle thousands of requests at the same time?

    Your JavaScript runs on one main thread, but slow work like network and file I/O is handed to the operating system or libuv's thread pool. The main thread never sits waiting; it keeps taking new requests and runs each callback when its I/O finishes.

    BeginnerIn-depth3 min read
  2. What is the difference between process.nextTick, setImmediate and setTimeout in Node.js?

    process.nextTick runs right after the current operation, before promise callbacks and before the event loop moves on. setImmediate runs in the check phase, right after I/O polling. setTimeout(fn, 0) runs in the timers phase after at least about 1 ms.

    IntermediateIn-depth3 min read
  3. What happens when you run a heavy CPU loop inside a Node.js request handler?

    The single JavaScript thread is busy, so the event loop cannot serve any other request until the loop ends. Every user sees delays. Move heavy work to worker threads, a queue, or break it into chunks.

    BeginnerQuick answer1 min read
  4. Predict the output: setTimeout, a promise, process.nextTick and plain logs together

    Synchronous code runs first, then process.nextTick callbacks, then promise callbacks (microtasks), then timers. So the order is sync, nextTick, promise, setTimeout.

    IntermediateQuick answer1 min read
  5. What are the main phases of the Node.js event loop?

    Timers, pending callbacks, idle/prepare, poll, check, and close callbacks. Timers run setTimeout callbacks, poll collects I/O, check runs setImmediate, and microtasks are drained between steps.

    IntermediateQuick answer1 min read