Cracking the Senior Node.js Backend Interview
Technical interviews for Node.js backend positions have shifted dramatically over the past two years. Standard trivia questions like “What is Node.js?” or “What are callbacks?” have been replaced by rigorous architectural evaluations: event loop microtask mechanics, memory leak triage, worker thread parallelism, database connection lifecycle management, and high-concurrency stream pipelines.
Whether you are interviewing for a Senior Backend Engineer, Tech Lead, or Distributed Systems role, demonstrating deep understanding of V8 internal mechanics, libuv execution phases, and production reliability separates top candidates from the rest.
Here are the top 25 Node.js backend coding and architectural interview questions, complete with code samples, runtime explanations, and senior-level insights.
Core Concurrency & Event Loop Questions
1. What is the exact execution order of microtasks vs. macrotasks in Node.js?
Answer: The event loop processes macrotasks across 6 distinct libuv phases: Timers, Pending Callbacks, Idle/Prepare, Poll, Check (setImmediate), and Close Callbacks. However, between every single phase transition (and after every callback execution in modern Node.js), the runtime drains the Microtask Queue.
Crucially, the microtask queue has two sub-queues with strict priority:
process.nextTick()queue (runs first with highest priority).- Promise microtasks queue (resolved Promises /
queueMicrotask).
// Prediction Quiz:
console.log('1: Sync');
setTimeout(() => console.log('2: Timeout'), 0);
setImmediate(() => console.log('3: Immediate'));
process.nextTick(() => console.log('4: nextTick'));
Promise.resolve().then(() => console.log('5: Promise'));
console.log('6: Sync End');
// Output:
// 1: Sync
// 6: Sync End
// 4: nextTick
// 5: Promise
// 2: Timeout (or 3 depending on timer registration loop entry)
// 3: Immediate2. How does Node.js handle CPU-bound tasks without blocking the main event loop?
Answer: Because JavaScript execution runs on a single main thread, CPU-heavy tasks (e.g. image processing, cryptographic hashing, matrix multiplication) block the event loop, causing all pending HTTP requests to timeout. Top architectures address this in three ways:
- Worker Threads (
worker_threads): Spawns true OS threads sharing memory viaSharedArrayBufferfor parallel CPU computations. - Cluster Module: Forks multiple Node.js child processes sharing the same server port using OS-level IPC socket passing.
- Offloading to Message Queues: Pushing compute-heavy jobs to background workers (e.g. BullMQ, Celery, or Go worker services).
3. How do you identify and fix a memory leak in a Node.js production service?
Answer: Follow a structured 4-step triage methodology:
- Observe Metrics: Track Resident Set Size (RSS) and Heap Used via Prometheus or CloudWatch. If Heap Used exhibits a saw-tooth pattern with an ever-increasing floor, memory is leaking.
- Generate Heap Snapshots: Take two heap snapshots at different times using
v8.getHeapSnapshot()or Chrome DevTools remote inspector. - Compare Snapshots: Look for objects with high Retained Size. The common culprits in Node.js are:
- Global arrays or maps caching objects without TTLs (use
WeakMapor LRU caches). - Dangling event listeners (calling
emitter.on()inside request handlers withoutremoveListener()). - Closures capturing large parent scopes.
- Global arrays or maps caching objects without TTLs (use
- Fix & Verify: Implement proper cleanup hooks and confirm memory stabilizes under simulated k6 load testing.
4. What is the difference between path.join() and path.resolve()?
Answer: path.join() simply concatenates path segments using the platform-specific separator and normalizes relative segments (like .. and .). path.resolve() processes segments from right to left until an absolute path is formed, effectively simulating the shell cd command starting from the current working directory (process.cwd()).
5. How does Node.js stream backpressure work and why does pipe() fail in enterprise apps?
Answer: Backpressure occurs when a readable stream pushes data faster than a writable stream can consume it. When writable.write(chunk) returns false, the readable stream must pause reading until the writable emits the drain event. While readable.pipe(writable) handles backpressure, it does not forward errors or close streams properly when an error occurs, leading to orphaned sockets and file descriptor leaks. Always use stream/promises.pipeline().
Architectural & Coding Solutions Summary Table
| Topic / Question | Key Senior Talking Point | Common Anti-Pattern |
|---|---|---|
| Uncaught Exceptions | Log error, close active keep-alive sockets, and exit with code 1 (PM2 restarts process). | Catching error with process.on('uncaughtException') and continuing execution in corrupted state. |
| Cluster vs Worker Threads | Cluster runs isolated memory spaces; Worker Threads share memory buffers. | Using Worker Threads for I/O operations instead of CPU-bound computations. |
| Asynchronous Error Propagation | Always return or await Promises inside async functions; use centralized Express error middleware. | Forgetting to return inside a Promise callback, creating floating unhandled rejections. |
| Graceful Shutdown | Trap SIGTERM/SIGINT, call server.close(), drain DB pool, set 30s timeout. | Instantly calling process.exit(0), abruptly severing customer checkout transactions. |
Frequently Asked Questions (FAQ)
What is the libuv thread pool default size and when is it used?
The default thread pool size is 4 threads (configurable up to 1024 via UV_THREADPOOL_SIZE). It is used for synchronous file system I/O, DNS lookups (dns.lookup), and CPU cryptographic operations (crypto.pbkdf2). Network socket I/O does NOT use the thread pool; it uses OS non-blocking epoll/kqueue polling.
Why is process.nextTick() dangerous if used recursively?
Because the process.nextTick() queue is completely drained before the event loop advances to the next phase, recursive calls to nextTick() completely starve the event loop, preventing all I/O events, timers, and HTTP requests from ever executing.
Related Architectural Deep Dives
Continue expanding your technical depth with our related engineering guides:
- Node.js Event Loop Architecture: Deep dive into libuv phases, microtasks vs macrotasks, and worker threads. Explore the complete guide: Node.js Event Loop Architecture.
- Docker Handbook for Backend Developers: Containerizing Node.js applications with multi-stage builds and Alpine. Explore the complete guide: Docker Handbook for Backend Developers.
Published by Mohit Kumar under Technical Interview Prep. Sharpen your knowledge with our guide on Mastering Node.js EventEmitter.
Related Modular Guides & Next Steps
- Stream Processing & Memory: Explore Node.js Streams & Pipelines: Processing Gigabyte Files Without Memory Spikes.
- Concurrency & Worker Threads: Explore Multithreading in Node.js: Worker Threads vs Cluster Module vs Child Processes.
- Production Security Standards: Explore Node.js Security Hardening Checklist: Defense Against Prototype Pollution, XSS & Injection.
- Distributed Inter-Service Comms: Explore Microservices Communication in Node.js: REST vs gRPC vs Message Brokers (RabbitMQ).




