Skip to main content

Posts

Node.js interview questions set 1

  Let start with set 1, 1)error-first callback in node?  This question is asked many times don't know what special it is. Ans: The pattern used across all the asynchronous methods in Node.js is called Error-first Callback. Here is an example: fs.readFile( "file.json", function ( err, data ) {   if ( err ) {     console.error( err );   }   console.log( data ); }); Any asynchronous method expects one of the arguments to be a callback. The full callback argument list depends on the caller method,  but the first argument is always an error object or null.  When we go for the asynchronous method, an exception thrown during function execution cannot be detected in a try/catch statement. The event happens after the JavaScript engine leaves the try block. In the preceding example, if an exception is thrown during the reading of the file,  it lands on the callback function as the first and mandatory parameter. 2)What is a stub?  Ans: Stubs are functions/programs that simulate the

Rapid fire 28 javascript questions and answers .

  Check these rapid javascript questions and answers that will help you to understand Javascript in a different way. Question 1: What is typeof [] ? Ans: Object. Actually, Array is derived from Object. If you want to check the array use Array.isArray(arr) Question 2: What is typeof arguments? Ans: Object. arguments are array-like but not array. it has length, can access by index but can't push pop, etc. Question 3: What is 2+true? Ans: 3. The plus operator between a number and a boolean or two boolean will convert boolean to number. Hence, true converts to 1 and you get a result of 2+1 Question 4: What is '6'+9 ? Ans: 69. If one of the operands of the plus (+) operator is a string it will convert other numbers or boolean to string and perform concatenation. For the same reason, "2"+true will return "2true" Question 5: What is the value of 4+3+2+"1" ? Ans: 91 . The addition starts from the left, 4+3 results 7 and 7+2 is 9. So far, the plus o

Mongodb Basics Set 1: Insert Operation

  In this series, I would be sharing a few basic MongoDB articles on different operations. 1 )Insert a single record in MongoDB collection . var param = { firstname: "joey" , middlename: "" , lastname: "tribani" , phone: 9898989898 } db . collection( 'exampleset' ) . insertOne(param) This returns something like this: { "acknowledged" : true, "insertedId" : ObjectId( "56fc40f9d735c28df206d078" ) } You can also use 'insert' commond for inserting single record . db . collection( 'exampleset' ) . insert(param); This returns : WriteResult({ "nInserted" : 1 }); 2 )Insert multiple records in MongoDB collection . var records = [{ firstname: "joey" , middlename: "" , lastname: "tribani" , phone: 9898989898 },{firstname: "koey" , middlename: "" , lastname: "desuoiza" , phone: 9898989898

Coding interview questions set 7

Let's begin with set 7, Q15) Best Time to Buy and Sell Stock You are given an array of prices where prices[i] is the price of a given stock on an ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.   Solution: var maxProfit = function (prices) { var min = Number .MAX_SAFE_INTEGER; var max = 0 ; for ( var i = 0 ; i < prices.length; i ++ ) { min = Math .min(min, prices[i]); max =

Data structure problem 2 (Stack): Valid Parentheses problem.

This is one of the interview questions asked in the technical or data structure round which can be solved using stack. As we know  stack is first in last out . You may also like this: Data structure problem 1.  So let's begin with the problem statement, Given a string s containing only the characters (, ), {, }, [ and ],  determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Examples : a)Input: s = "()" Output: true Example 2: b)Input: s = "()[]{}" Output: true Example 3: c)Input: s = "(]" Output: false Solution: var isValid = function (s) { const obj = { "}" : "{" , "]" : "[" , ")" : "(" , }, var stack = []; //As javascript array behaves like stack for ( let i = 0 ; i < s.length; i ++ ) { if (s[i

Create a multi column csv report in nodejs without external library.

Recently I had a requirement to create a CSV report with multiple columns(merging two or more reports) in a single CSV file, but I didn't find any particular library(Maybe I didn't search properly) but found this solution.  So let's begin with code that is self-explanatory. const fs = require( 'fs' ); const _file = ` Agent_Customer_Report.csv ` //Report 1 const header1 = [ 'Customer Name' , 'DOB' , 'City' , 'Pincode' ]; const field1 = [ 'custName' , 'dob' , 'city' , 'pincode' ]; const report1Data = [{ 'custName' : "ABHISHEK" , 'dob' : '01/23/1992' , 'pincode' : '443221' , 'city' : "Mumbai" }{ 'custName' : "PUNIT" , 'dob' : '01/23/1992' , 'pincode' : '443221' , 'city' : "Mumbai" }] //Report2 const header2 = [ 'Agent Name' , 'DOB' , 'City

Javascript interview questions set 3

  For set 2 please refer to the  Javascript interview question set 2. Let's begin with set 3, 5)Different Array methods? a)Array map() Method? .map() It is used when you want to transform elements in an array. The map() method creates a new array with the results of calling a function for every array element. The map() method calls the provided function once for each element in an array, in order. Note: this method does not change the original array. Ex:var numbers = [65, 44, 12, 4]; var newarray = numbers.map(myFunction) function myFunction(num) {   return num * 10; } b)When to use the filter method? .filter()  when you want to select a subset of multiple elements from an array. The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not change the original array. Ex: var ages = [32, 33, 16, 40]; function checkAdult(age) {   return age >= 18; } ages.filter(checkAdult); c)When to use find()? .find() When