How To Crack Node.js Interview ?

Node.js popularity has been all time high and so is the need for good Node.js developers. I have been to some node.js/javascript interview’s and I am writing this blog to share my experience.

So what interviewer is looking for a node.js person?

Javascript plays a key role in Node.js so you should be very well prepared on javascript. It is primarily used for doing server side programming. In a typical example calling a web service or doing I/O to and from a database.

Understanding of Express.js concepts: Express is official node.js web framework so it very important to understand this well. Some of the area you should focus: Routing, Post & Get, Different type of response code in express, middleware etc.

JavaScript:

Javascript was written in just 10 days in May 1995 by Brendan Eich. 10 days is quite short time for a programming language which is killing the industry right now. In 1996 – 1997 JavaScript was taken to ECMA to carve out a standard specification, which other browser vendors could then implement based on the work done at Netscape. JavaScript was historically known as language of the browser until Node.js was born. Then people start using it for server side too. So today full stack development in Javascript is possible.

This video from Douglas Crockford touches all the good parts for javaScript(if you are not already familiar).

What is inheritance model in javascript ?

Inheritance is concept where child class can inherit the properties of parent’s class. There are two types of inheritance in javascript.

What is a closure?

Closure is concept in javascript where a function can return a function.The pattern of public, private, and privileged members is possible because JavaScript has closures. What this means isInner functions will always have access to var and parameters of outer function even after when outer function is returned.

What is type inference? can you explain the difference between the == operator and the === operator?

Type inference is when the javascript interpreter tries to determine the type of a reference in the runtime. “==” performs type conversion before evaluating the comparison and “===” is used for strict comparison (will return false if type of two values which are to compared does not match).

What is the use of “use strict” ?

In normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.

Few examples:

“use strict”;
x = 2; // This will cause an error (x is not defined)

“use strict”;
function x(p1, p1) {}; // This will cause an error

What is the use of this keyword ?

This keyword is generally used to refer properties of an object that the function is bound to. This keyword is usually used inside a function. It can be used outside the function too in the global scope but its not recommended to use this in this way. In strict mode this refers to undefined.

Express JS

Express is highly opinionated web application for Node.js application. Express is commonly used to creating an API layer for your application but it can also be used for rendering an html page. Many popular framework are built on express.

Set the response status code and send its string representation as the response body.

What is middleware in express ?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Node.js

Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

What is event loop in Node.js ?

A JavaScript runtime contains a inbuilt message queue, which is a list of messages to be processed. In a typical node.js environment each message is associated with a call back function. When the stack is empty, a message is taken out of the queue and processed. The processing consists of calling the associated callback function. The message processing ends when the stack becomes empty again and this process is repeated.

Handling I/O in node.js is typically performed via events and callbacks, so when the application is waiting for a database to insert/update to finish, it can still process other things like user input.

Is Node.js single threaded ? How can you make it multithreaded?

Yes. You can make it multithreading by using a node cluster.

What is clustering in Node.js?

A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.

What is NPM?

npm is packaging ecosystem for node.js, is the largest ecosystem of open source libraries in the world.

What is Grunt/Gulp?

Grunt or Gulp both are popular task runner for node.js application. It is usually used to group tasks like minification, compilation, unit testing, linting etc to be done in automated fashion.

Bonus questions:

What are promises ?

As Node.js become popular it has become very critical for javascript programmers to write asynchronous code. Native javascript prior to version 1.5 does not provide a good way to write asynchronous code. Programmers were forced to use callback from a callback which usually end up in un maintainable code. This concept is also called as callbackhell. In earlier node.js days Q library was very popular which makes it easier to write asynchronous code. Today, ES6 natively supports Promises.

The Promise object is used for asynchronous computations. A Promise represents an operation that hasn’t completed yet, but is expected in the future.It allows you to associate handlers to an asynchronous action’s eventual success value or failure reason.

ES6 ?

ES6 is the new way for writing javascript. A lot of new features is added with ES6 to make javascript feels like a better programming language. Node version greater than v4 supports ES6 natively. This make it easier to use all new features. Read more about ES6 here.

Testing JavaScript Application ?

Mocha and Jasmine are two most popular testing framework which are very well integrated with node.js.

✓ Both support Asynchronous Tests
✓ Both support CI (continuos integration) build. Can be easily integrated with Jenkins.
✓ Both allows you to organize tests in suits
✓Supports both client-side and server-side testing

Coding exercises:

This is the most favorite JS assessment exercise which interviewers usually ask. I would suggest to come up with your own creative solutions to the exercise instead of just going the solution. This is my solutions to the exercise. The goal of this exercise is to pass as many test cases in the limited time.

Algorithm Style Questions:

Usually you will be asked to solve a algorithm style question. Most of the companies if you are applying for contract role usually don’t usually ask for Big O analysis. If you can solve the problem in the given time you will get a job. I have listed all the common question in my github repo that i have seen so far. Other places where you can practice these type of questions for free are hackerrank and leetcode.

JavaScript Fundamental Questions:

Some companies might also ask you to write a code that use inheritance in javascript or write a code that uses a closure. The goal here is test javascript fundamentals. Here is list of questions with solutions that I have seen usually in a interview.