Skip to main content

Posts

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

CSS INTERVIEW QUESTIONS SET 2

  You make also like this CSS interview question set 1. Let's begin with set 2, 5)What is the difference between opacity 0 vs display none vs visibility hidden? Property           | occupies space | consumes clicks | +--------------------+----------------+-----------------+ | opacity: 0         |        yes      |        yes       | +--------------------+----------------+-----------------+ | visibility: hidden |        yes       |        no        | +--------------------+----------------+-----------------+ | display: none      |        no       |        no        | When we say it consumes click, that means it also consumes other pointer-events like onmousedown,onmousemove, etc. In essence "visibility: hidden" behaves like a combination of "opacity: 0" and "pointer-events: none". 6)CSS 3 new features? CSS3 allows developers to style HTML elements easier.  They are less dependent on image files  and can complete CSS styling with fewer lines of code. CSS3

Coding Interview questions set 6

Let's begin with set 6, 13)Given an array of integer nums and an integer target, return indices of the two numbers such that they add up to the target. Assumptions:  Each input would have exactly one solution, and you may not use the same element twice. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Solution: const twoSum = (nums, target) => {   const map = {};   for (let i = 0; i < nums.length; i++) {     const another = target - nums[i];     if (another in map) {       return [map[another], i];     }     map[nums[i]] = i;   }   return null; }; You may also like these articles:   Coding Interview Set 1     Coding Interview Set 2 .     Coding Interview Set 3   Coding Interview Set 4 14)Given an array of numbers containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Could you impleme

Node.js: create an excel file with multiple tabs.

This article is a sample code to generate an excel file with multiple tabs using  excel4node  module. Install: npm i excel4node Let's see the sample code, var excel = require( 'excel4node' ); // Create a new instance of a Workbook class var workbook = new excel.Workbook(); // Add Worksheets to the workbook var worksheet = workbook.addWorksheet( 'Buy order types' ); var worksheet1 = workbook.addWorksheet( 'Sell order types' ); // Style for headers var style = workbook.createStyle({ font : { color : '#EA3A14' , size : 18 }, numberFormat : '$#,##0.00; ($#,##0.00); -' }); var styleForData = workbook.createStyle({ font : { color : '#47180E' , size : 12 }, alignment : { wrapText : true , horizontal : 'center' , }, numberFormat : '$#,##0.00; ($#,##0.00); -' }); let buyOrderTypes = [ {name : "buy" ,id : "1" ,comment : "Normal