Skip to main content

Posts

Node.js interview questions set 2

  Let start with set 2, 5)What is buffer in node.js? Ans:  Node.js includes an additional data type called Buffer (not available in browser's JavaScript).  Buffer is mainly used to store binary data while reading from a file or receiving packets over the network. Buffer class is a global class that can be accessed in an application without importing the buffer module. Ex:var buf = new Buffer(10); 6)Explain the concept of URL module in Node.js? Ans: The URL module in Node.js splits up a web address into readable parts.  Use require() to include the module: var url = require( 'url' ); Then parse an address with the url . parse() method, and it will return a URL object with each part of the address as properties . var url = require( 'url' ); var adr = 'http://localhost:8080/default.htm?year=2021&month=september' ; var q = url . parse(adr, true); console . log(q . host); // returns 'localhost:8080' console . log(q . pathname); // retur

Coding interview questions set 8

 Let's begin with set 8, 16)If given a max weight as input find out max allocation from a given array of jewelry weights. Example 1: Input: maxWeight=5 weightArray=[5,2,6] output: [{weight:5,qnt:1 }] Example 2: Input: maxWeight=12 weightArray=[5,2,6] output: [{weight:5,qnt:2},{weight:2,qnt:1}] Solution: function findCombination(maxWeight,weightArray) { var myArray = weightArray; let finalArray = []; for (let i = 0 ; i < myArray . length; i ++ ) { let effectiveWeight = myArray[i]; let remainder = maxWeight % effectiveWeight; let quotient = Math . trunc(maxWeight / effectiveWeight); if (quotient > 0 ) { finalArray . push({ weight: myArray[i], qnt: quotient }); } if (remainder == 0 ) { break ; } else { maxWeight = maxWeight - quotient * effectiveWeight; } } return finalArray; } Logic: Here we get quotient and remainder and p

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