Backend · Fresher-relevant
Node.js interview questions on event loop, streams, async patterns, Express.js, and REST API development.
Explain the Node.js event loop.
Tip: Node uses a single-threaded event loop (libuv) with phases: timers, pending callbacks, poll, check (setImmediate), and close. Blocking work is offloaded to a thread pool. process.nextTick and Promise microtasks run between phases, before timers. Never block the loop with synchronous CPU work.
What is the difference between process.nextTick() and setImmediate()?
Tip: process.nextTick() callbacks run immediately after the current operation, before the event loop continues — they can starve I/O if overused. setImmediate() runs in the check phase, after the current poll phase completes. Use setImmediate for "run after I/O", nextTick sparingly for cleanup.
How do you handle CPU-intensive tasks in Node.js without blocking?
Tip: Offload to worker_threads (shared memory, good for CPU work), child_process for separate processes, or the cluster module to fork the server across CPU cores. For heavy compute, a job queue (BullMQ + Redis) processed by separate workers keeps the request path responsive.
What is middleware in Express.js?
Tip: Middleware are functions with (req, res, next) that run in order on the request pipeline — used for logging, body parsing, auth, and error handling. Calling next() passes control onward; error-handling middleware takes four args (err, req, res, next) and is registered last.
What are Streams in Node.js and why use them?
Tip: Streams process data in chunks instead of buffering it all in memory — Readable, Writable, Duplex, and Transform. Piping (readable.pipe(writable)) handles backpressure automatically. Use them for large files, HTTP bodies, and data transformation to keep memory flat.
How does error handling differ for synchronous, callback, and async/await code?
Tip: Synchronous code uses try/catch. Callbacks use the error-first convention (cb(err, data)). Promises use .catch(); async/await uses try/catch around awaited calls. Unhandled promise rejections crash the process in modern Node — always attach a handler or wrap in try/catch.
What is the difference between require (CommonJS) and import (ES Modules)?
Tip: require is synchronous, dynamic, and resolved at runtime (CommonJS). import is static, hoisted, supports tree-shaking, and can be asynchronous (ESM). ESM is enabled via "type":"module" or .mjs. You cannot freely mix them; interop requires createRequire or dynamic import().
How do you prevent and detect memory leaks in a Node.js service?
Tip: Common causes: unbounded caches, uncleared timers, and growing global arrays or event-listener accumulation (watch for MaxListenersExceededWarning). Detect with heap snapshots in Chrome DevTools / clinic.js and by monitoring process.memoryUsage().heapUsed over time. Bound caches with an LRU and remove listeners on teardown.
InterviewEra generates role-specific questions using your actual projects and skills. Get scored feedback on technical depth, clarity, and structure — free to start.