Node.js Interview Questions and Answers

I have been working on Node for more than 5 years ( 2 years in academic and 3+ years professionally ). During this period of time, I have taken several interviews and attended interviews in various companies ranging from start-up such as Browserstack, Housing to giants such as Amazon.

I have authored a book on Sails.js which is an MVC framework for Node. In this post, I am going to summarize the questions along with answers for questions I have been asked during interviews and questions which I think as per my experience is good for your knowledge base.

Let’s begin.

What is Node? and Why to use it

Node.js is JavaScript run time framework built on top of Google V8 engine. One of the key reason to use Node is to perform asynchronous I/O operation.

What is the area of problem Node addresses

Key areas of problem Node addresses:

  • Slow web servers due to synchronous I/O.
  • Multithreaded programming bottlenecks.
  • Scaling.
  • Package management and deployment.

Why Node uses Google V8 Engine

Google V8 is Chrome runtime engine which converts JavaScript code into native machine code which in turn provides a fast running application.

Can we use other engines than V8

Yes. Microsoft Chakra is another JavaScript engine which can be used with Node.js. It’s not officially declared yet.

If Node is single threaded then how it handles concurrency

Node provides a single thread to programmers so that code can be written easily and without bottleneck. Node internally uses multiple POSIX threads for various I/O operations such as File, DNS, Network calls etc.

When Node gets I/O request it creates or uses a thread to perform that I/O operation and once the operation is done, it pushes the result to the event queue. On each such event, event loop runs and checks the queue and if the execution stack of Node is empty then it adds the queue result to execution stack.

This is how Node manages concurrency.

Explain event loop

As said, Node.js is single threaded but it supports concurrency using events and callbacks. The event loop is a component which checks event queue on a periodic basis and pushes the result in execution stack if the stack is empty.

What is callback hell

The asynchronous function requires callbacks as a return parameter. When multiple asynchronous functions are chained together then callback hell situation comes up. Consider the example code below.

This situation is referred to as “callback hell” situation.

Which is your preferred way to write asynchronous code in Node

Here is a list of methods I generally use to avoid callback hell and write asynchronous code.

  • Slow web servers due to synchronous I/O.
  • Multithreaded programming bottlenecks.
  • Scaling.
  • Package management and deployment.

I personally prefer Async npm module.

What is stream and explain types of streams

Streams are a collection of data that might not be available all at once and don’t have to fit in memory. Streams provide chunks of data in a continuous manner. It is useful to read a large set of data and process it.

There is 4 fundamental type of streams:

  1. Readable.
  2. Writeable.
  3. Duplex.
  4. Transform.

Readable streams as the name suggest used in reading a large chunk of data from a source. Writable streams are used in writing a large chunk of data to the destination.

Duplex streams are both readable and writable ( Eg socket). Transform stream is the duplex stream which is used in modifying the data (eg zip creation).

Explain child processes in Node

Child process module enables us to access operating system functionaries. Scalability is baked into Node and child processes are the key factors to scale our application. You can use child process to run system commands, read large files without blocking event loop, decompose the application into various “nodes” (That’s why it’s called Node).

Can we send/receive messages between child processes

Yes, we can.

We can use send() function to send message to workers and receive the response on process.on(‘message’) event.

Explain file system module of Node

Node **fs **module provides simple file system module to perform files related operation. This module comprises of synchronous and asynchronous functions to read/write files. For example, **readFile() **function is asynchronous function to read file content from specified path and **readFileSync() **is synchronous function to read files.

How to scale Node application

We can scale Node application in following ways:

  • Slow web servers due to synchronous I/O.
  • Multithreaded programming bottlenecks.
  • Scaling.
  • Package management and deployment.

List down your favorite and most useful NPM library

You should list down your favorite node module along with reasons.

How to deploy Node application

You should know how to deploy Node application on various cloud providers. If you know the basics, such as SSH access, git cloning and running the application in process manager then more or less the steps are same in various cloud providers.

Reference: Hosting Node.js app to DigitalOcean Server

We will keep adding questions as found or suggested by readers.

You should also learn or revise few core JavaScript basics such as:

  • Slow web servers due to synchronous I/O.
  • Multithreaded programming bottlenecks.
  • Scaling.
  • Package management and deployment.

Books Recommendation

These books might help you to prepare for your next Node.js interview.

  • Slow web servers due to synchronous I/O.
  • Multithreaded programming bottlenecks.
  • Scaling.
  • Package management and deployment.

You can also prepare and learn from our Node.js tutorials list.

Conclusion

We will be updating this article often so it would be best if you bookmark it. A node.js interview could be tricky and mainly people get confused in event loop and concurrency. Hope you like these questions and answers and I hope it helps you in preparing for next interview. Good luck, btw

Recommended Reading

Deploying NestJS Apps to Zeit Now

Convert SVG to PNG Using NodeJS & Sharp

Building a Node API with Stateless Authentication

Quick Getting Started with ElectronJS

ABC’s of JavaScript and Node.js

Learn Nodejs by building 12 projects

NodeJS & MEAN Stack - for Beginners - In Easy way!

#node-js #javascript

Node.js Interview Questions and Answers
12.55 GEEK