Coding Interview Questions: Node.js and more
What is “callback hell” and how can it be avoided?
Callback hell refers to the phenomenon of one function that takes a callback function as an argument, that callback function itself taking a callback function as an argument, the new callback taking a callback function as an argument also: essentially many nested callbacks for which the relationship between them can become intractable to keep track of. A good practice to avoid callback hell is by declaring callback functions and saving them to variables before they are called, so that it is easier to retrace the logical flow of these functions once they are called. One practical example that comes to mind is named controller functions that are then exported to be used as one of the callbacks in router functions with express.js.
What are “stubs” in Node.js?
Stubs are functions that act as proof of concept testing “placeholders” in code. They can act as a simple stand in method that produces the return value of the actual method to be written later that will be much later, or they can act as a dummy stand-in object in the same way. One practical use case would be testing database handling before the actual database for a large-scale project exists. More detail here.
What are “streams” in Node.JS?
Streams are one of the essential features of Node.js. Streams are interfaces or handling the flow and transfer of data in node by facilitating both reading inputs from and writing outputs to files. Streams as such are essentially and API, and Node.js includes a native stream API. An advantage of streams is that they help an application efficiently handle large sets of data or data coming in chunks from external sources, without all the the data having to fit in memory at one time. For more, see this great article from Samer Buna.
What does “chaining” mean in Node.JS?
“Chaining” in Node.js refers to chaining asynchronous functions together to assure that a given function will not run until the previous function it depends on has completed. A common application of chaining is in the case of chaining multiple promises together, so that each promise will run only once the promise prior to it in the chain has itself resolved, and so on.
Explain “console” in Node.JS.
Console in Node.js is a global object that is used for printing messages to standard output (stdout) and standard error (stderr). Unlike in JS running in the browser, these messages don’t print to the browser console, but rather to the terminal, a file, or to a pipe (a one way communication from one process to another). Hence Node console object has the .log method, but also several others, which are listed here.
Explain exit codes in Node.JS. List out some exit codes.
Exit codes are given as a Node.js application is terminated. The default exit code in node is 0, which indicates there are no longer any pending asynchronous operations and hence the program was terminated cleanly. The other exit codes indicate that the termination was not “clean” in this way. A few of them are:
1Uncaught Fatal Exception: There was an uncaught exception, and it was not handled by a domain or an
5Fatal Error: There was a fatal unrecoverable error in V8. Typically a message will be printed to stderr with the prefix
13Unfinished Top-Level Await:
awaitwas used outside of a function in the top-level code, but the passed
Source, which also includes a full list of exit codes: node.js
What is the difference between cluster and non-cluster Index?
A clustered index stores data in a 1:1 key-value mapping according to the order of the data. An example is a phone book organized alphabetically from A-Z. A clustered index can be created only on a primary key, where no two of these keys can have the same value. Accordingly, clustered indexes don’t require any additional space in memory than the length of the dataset, and are faster performing for data access. An Array object in program is a straightforward analogue to a clustered index.
A non-clustered index, on the other hand, need not maintain this “1:1” relationship. Clustered indexes can be created on any key in the table (not just the primary key), and there can be multiple clustered indexes in one table. However, this means that extra space is needed to store data structures involving clustered indexes. While clustered indexes are faster for data access, non-clustered indexes are faster performing for updating data. A linked list data structure in programing is a good example of non-clustered indexing.
What are user defined functions? What are all types of user defined functions?
User defined functions, as opposed to built-in functions or methods, are custom functions written to perform a particular task in the coding language or development environment one is working with. The four types of user defined functions are:
- Having no arguments and no return value
- Having arguments and a return value
- Having arguments and no return value
- Having no arguments and a return value