NodeJS interview questions

Jibin Mathews
The Bit Theories
Published in
4 min readOct 1, 2016

--

Over the course of my Internship with a startup as a NodeJS Developer, I was asked to take first round of interviews for full time hiring. So my surprise people with 2–3 years of experience with NodeJS are not able to answer basic questions related to NodeJS scripts. Here are few questions with brief answers which I feel should be known by every NodeJS developer.

1. What is event loop in JavaScript?

Event loop, as the name suggests is loop which runs in background listening for various event emitters.

2. What is a callback?

Callback is a piece of code which is passed as an argument to another function (say fn1) to execute the callback function (say fn2) after executing the function fn1. This is used to handle the asynchronous behaviour of javascript.

The functionality of a callback can be imagined as somewhat analogous to Interfaces in JAVA.

3. What is callback hell and how can it be avoided?

Callback hell refers to a coding pattern where there is a lot of nesting of callback functions. The code forms a pyramid like structure and it becomes difficult to debug.

It can be avoided by:

a. Using promises
b. Yield operator and Generator functions from ES6
c. Modularizing code
d. Using
async library (http://caolan.github.io/async/)

4. What is a Promise?

A promise is a method that eventually produces a value. Instead of passing a function as argument as with callbacks, “Once the result is received from an asynchronous operation then the required function is executed”. The required functions are never passed as arguments to the asynchronous operation.

It has the form:

callAsyncFunction()
.then(firstFunction)
.then(secondFunction)
.then(thirdFunction)
.then(fourthFunction);

Same thing using callbacks

callAsyncFunction( function(err, result) {
firstFunction(err1, result, function(result2){
secondFunction(err2, result2, function(result3){
thirdFunction(err3, result3, function(result4){
fourthFunction(err4, result4){
//Final code here
});
});
});
});
});

P.S. This pyramid like code structure depicts a Callback Hell. It can make debugging very difficult in large projects.

5. What are ACID properties wrt to a database?

ACID stands for Atomicity, Consistency, Isolation and Durability.

Atomicity

It means that either the record/document updates completely or does not update with respect to the operation.

Either everything occurs or nothing occurs.

Consistency

It implies that a transaction either creates a new and valid state of data, or, if any failure occurs, returns all data to its state before the transaction was started.

If successful then valid data else revert back to original data.

Isolation

Two or more simultaneous database queries should run independent of the other transactions.

Durability

Data once committed should be saved in memory so that even in case of system failure, data is not affected.

This article on GeeksforGeeks explains this concept very nicely.

6. Where and why should you not use NodeJS?

Now this one is pretty interesting question. I have heard answers like:

“NodeJS should not be used because it is not secure as other low level language based web servers as it uses JavaScript which is a client side scripting language”

To everyone out there, this is NOT the reason. You can make NodeJS servers as secure as you want. It depends on your requirements and your code.

To answer this question, NodeJS should NOT be used where computations are CPU intensive. Eg: Data Analytics Server, Image Processing Servers, Video Processing Servers etc.

NodeJS is meant for highly I/O-bound operations which does not require heavy CPU intensive operation/tasks.

Some Additional facts which you may be asked:

  1. MongoDB is not ACID compliant
    This means that MongoDB does not guarantee any of the ACID properties.
  2. NodeJS is a Runtime Environment. It is NOT a JavaScript framework.

Also see NodeJS Assignment Tips.

All the Best !!!

Please click the “Follow” button to learn easily via “The Bit Theories

Team Cyber Labs (Website, Facebook)

Don’t forget to Recommend & Share :)

--

--