Skip to main content

Posts

Showing posts from April, 2021

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