Skip to main content


Nodejs interview questions set 3

  1)What is nonblocking io? Ans:  Non-blocking I/O operations allow a single process to serve multiple requests at the same time. Instead of the process being blocked and waiting for I/O operations to complete, the I/O operations are delegated to the system, so that the process can execute the next piece of code 2)Async await in foreach? Ans: Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.) For example, the following forEach loop might not do what it appears to do: const players = await this.getWinners(); // BAD await players.forEach(async (player) => {   await givePrizeToPlayer(player); }); await sendEmailToAdmin('All prizes awarded'); What's wrong with it? The promises returned by the iterator function are not handled. So if one of them throws an error, the error won't be caught. (In Node 10, if no unhandledrejection listener has been registered, that will cause the process to

MongoDB interview questions set 1

  1)How to find the second highest salary ?   var records =[     { salary : 100 . ..m, 00 },     { salary : 9000 },     { salary : 9000 },     { salary : 5000 },     { salary : 10000 },     ]     let counts = await _db . collection ( "salary" ). insertMany ( records ) Ans: Solution 1 let highest = await _db . collection ( "salary" ). find (). sort ({ salary : - 1 }). limit ( 1 ). toArray (); let secondhighest = await _db . collection ( "salary" ). find ({ salary : { $lt :counts [ 0 ]. salary }}). sort ({ salary : - 1 }). limit ( 1 ). toArray (); Solution 2: step:1 var sal = await _db.collection("salary").distinct("salary") output:[   5000,   9000,   10000, ] step:2 9000 is the second-highest salary 2)How to find if the query for finding records uses an index or not?     let highest = await _db.collection("salaryy").find({itd:{$gte:0}}).sort({salary:-1}).explain('executionStats'); explain query will give al

Coding interview set 9

  Let's begin with set 9, 18) Write a logic that takes input as  EC:B0:8T:E4 and response should be "0C:B0:80:04". Wrong solution is already provided find the issue in that? let mac = 'EC:B0:8T:E4' const mapper = {     E: 0,     T: 0 } Wrong solution: var decoded = "" ; for ( let item of mac){ decoded = decoded + mapper[item] != undefined ? mapper[item] : item; } console.log(decoded); The issue with this solution: + vs. ?: In JavaScript, the + operator has higher precedence than the ?: operator. This means that concatenation will take place before the condition in a ternary is evaluated. This can lead to some strange results.  Note: operator associativity and precedence can change between languages. For example, in JavaScript the ?: operator is right-associative but it's left-associative in PHP. These same comparisons will produce different results between these languages. One of the correct solutions with () as it has higher precedence : va

Data structure problem 3: Implement a Stack With Minimum

  Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. Input ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop();;    // return 0 minStack.getMin(); // return -2 Solution: var  MinStack =  function ( )  { this .elements = []; }; /** @param {number} x @return {void} */ MinStack.prototype.push = function ( x ) { this .element

Projection in MongoDB

In MongoDB, projection means selecting only the necessary data rather than  selecting the whole of the data of a document. If a document has 5 fields and  you need to show only 3, then select only 3 fields from them. You may also like MongoDb insert. 1 )Projection in find . Here 0 means not to project i . e exclusion and 1 means project i . e inclusion as per MongoDB . Projection cannot have a mix of inclusion and exclusion but only the _id field can exclude with inclusion . db . collection( "companies" ) . find({name: "Dvara SmartGold" },{projection: { _id: 0 , id : 1 , name: 1 } }) . toArray() Or db . collection( "companies" ) . find({name: "Dvara SmartGold" }) . project({_id: 0 , id : 1 , name: 1 }) . toArray() This will give us all documents inside companies collection as per filter with only name and id fields . 2 )Projection in findOne . db . collection( "companies" ) . findOne({name: &qu

Javascript event-loop and Nodejs event-loop.

The Event Loop in the Browser or Node is not part of V8. The Event Loop Is Part of a different application/dependency/library which is provided by the Browser or Node. They do not use V8's event loop. V8 does implement an event loop, it is there . However, it is meant to be overridden or replaced, which is something both Chrome and NodeJS happen to do. The Nodejs event loop implementation differs from the browser-based event loop one. While Nodejs uses the Google V8 as its runtime, it does not use V8 to implement the event loop. Nodejs uses the Libuv library (written in C) to implement the event loop.  Browser (Chrome):  V8 just executes your JavaScript (If and else statements, for statements, functions, arithmetic operations e.t.c) and then hands over operations to Libevent . In the Browser(e.g Chrome) apart from JavaScript Engine V8 (Chrome uses V8), the browser also contains different applications/dependencies/libraries which can do a variety of things like sending HTTP requests

LinkedIn JavaScript Skill Assessment 82 Questions.

  Q1. Which operator returns true if the two compared values are not equal? <> ~ ==! !== Q2. How is a forEach statement different from a for a statement? Only a for statement uses a callback function. A for statement is generic, but a forEach statement can be used only with an array. Only a forEach statement lets you specify your own iterator. A forEach statement is generic, but a for statement can be used only with an array. Q3. Review the code below. Which statement calls the addTax function and passes 50 as an argument? function addTax(total) { return total * 1.05; } addTax = 50; return addTax 50; addTax(50); addTax 50; Q3. How would you use this function to find out how much tax should be paid on $50? (Version 2, possibly an updated version) function addTax(total) { return total * 1.05; } addTax($50); return addTax 50; addTax(50); addTax 50; Q4. Which statement is the correct way to create a variable called rate and assign it the value 100? let rate = 100; let 100 = rate; 100