Top 10 Interview Questions

⭐ Interview Importance: HIGH
⏱️ Revision Time: 5 min

TL;DR

A quick reference guide to the absolute most common theoretical questions asked in mid-to-senior Node.js engineering interviews.

1. What is Node.js and how does it work?

Node.js is an open-source, cross-platform runtime environment that allows developers to execute JavaScript outside of a browser. It is built on Google Chrome’s V8 JavaScript engine. It is characterized by its single-threaded, asynchronous, non-blocking I/O model, managed by the Event Loop (via libuv).

2. Explain the Event Loop.

The Event Loop is the mechanism that allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. When an async operation (like reading a file) is called, Node.js passes it to the OS or the libuv thread pool. The main thread continues executing code. When the async task finishes, its callback is placed in a task queue, and the Event Loop pushes it back onto the main call stack when the stack is empty.

3. What is the difference between process.nextTick() and setImmediate()?

  • process.nextTick(): Executes its callback immediately after the current operation completes, but before the Event Loop moves to the next phase. It runs before Promise microtasks. It can starve the event loop if used recursively.
  • setImmediate(): Places its callback in the “Check” queue of the Event Loop. It executes after the current polling phase completes.

4. How does Node.js handle concurrency if it is single-threaded?

It handles concurrent I/O requests asynchronously using the Event Loop, allowing one thread to juggle thousands of network connections without blocking. For CPU-bound concurrency, Node.js uses the worker_threads module (multithreading) or the cluster module (multiprocessing).

5. What are Streams and why are they useful?

Streams are a way to process reading and writing data piece-by-piece rather than loading massive datasets completely into memory. This drastically reduces memory consumption (preventing OOM errors) and improves Time-To-First-Byte for network responses.

6. What is a Memory Leak and how do you find one in Node.js?

A memory leak occurs when the application retains references to objects that are no longer needed, preventing the Garbage Collector from freeing the memory. They are found by taking multiple Heap Snapshots using the V8 inspector and comparing them to see which objects are continuously growing in the “Retained Size” column.

7. Explain the difference between require() (CommonJS) and import (ESM).

  • require(): Synchronous, evaluated at runtime, wraps the file in an IIFE, native to older Node.js.
  • import: Asynchronous, parsed statically before execution, standard in browsers, supports Top-Level Await.

8. What is the role of package-lock.json?

It locks down the exact, specific versions (and cryptographic hashes) of every single dependency and transitive dependency installed in the project. This ensures that running npm install (or specifically npm ci) on another developer’s machine or in a CI/CD pipeline results in the exact same node_modules tree, preventing “works on my machine” bugs.

9. How do you scale a Node.js application?

  • Vertical: Increase the server’s RAM and CPU (and use --max-old-space-size).
  • Horizontal (Single Server): Use the cluster module or PM2 to spawn multiple Node.js processes to utilize all CPU cores.
  • Horizontal (Multi Server): Deploy stateless Node.js instances in Docker/Kubernetes behind a Load Balancer (NGINX), using Redis for shared sessions/caching.

10. What is Middleware in Express?

Middleware are functions that have access to the Request and Response objects. They run sequentially in a pipeline. A middleware can modify the request, end the response cycle, or pass control to the next middleware by calling next(). They are used for logging, authentication, and parsing body data.