Showing posts from October, 2020

Node.js (v.15) 4 things to know.

  The V8 JavaScript engine has been updated to V8 8.6  (V8 8.4 is the latest available in Node.js 14).  Along with performance tweaks and improvements the V8 update also brings  the following language features: 1)Promise.any(): Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfills,  returns a single promise that resolves with the value from that promise.  If no promises in the iterable fulfill (if all of the given promises are rejected),  then the returned promise is rejected with an AggregateError. Example: const promise1 = Promise.reject(0); const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick')); const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow')); const promises = [promise1, promise2, promise3]; Promise.any(promises).then((value) => console.log(value)); 2)AggregateError: The AggregateError object represents an error when several errors need to be wrapped i

Coding Interview Questions set 4

Let's begin with set 4, 9)Find the majority Element? Given an array of size n, find the majority element.  The majority element is the element that appears more than  n/2 times. Assumptions: You may assume that the array is non-empty and the majority element always exists in the array. Example 1: Input: [4,2,4] Output: 4 Example 2: Input: [3,3,1,1,1,3,3] Output: 3 Solution: var majorityElement = function(nums) { var obj = {}; for(var i = 0; i < nums.length; i++){ obj[nums[i]] = obj[nums[i]] + 1 || 1; if(obj[nums[i]] > nums.length / 2) { return nums[i]; }  }}; Logic: We have used an object map for maintaining the count.            You may also like this articles:   Coding Interview Set 1     Coding Interview Set 2 .     Coding Interview Set 3 10)Check if two strings are an anagram of each other? 2 words are anagram if 2nd word is formed by rearranging the letters from 1st word.   Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: