- remove_red_eye20992 Views
- event23 May 2019
- access_time5 min read
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.
We will keep updating this post as new questions comes up, hence it would be better if you bookmark it 🙂
What is Node? and Why to use it
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.
- Package management and deployment.
Why Node uses Google V8 Engine
Can we use other engines than V8
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.
- Using Async module.
- Using promises.
- Using function decomposition.
- Using async/await.
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:
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:
- cloning using cluster module.
- Decomposing the application into smaller services – i.e micro services.
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.
- Prototype chaining.
- How to create classes and perform inheritance.
These books might help you to prepare for your next Node.js interview.
You can also prepare and learn from our Node.js tutorials list.
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 🙂