Posts

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

Image
  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

Image
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:

Javascript interview questions set 2

Image
For set 1 please refer to the  Javascript interview question set 1. Let's begin with set 2, 3)What is the difference between Object.seal() and Object.freeze() in JavaScript? Object.seal() method protects the deletion of an existing property  but it can't protect the existing properties from outside changes.  var object1 = {       prop1: 1   };   Object.seal(object1);   object1.prop1 = 2; // value got changed   delete object1.prop1;   console.log(object1)   //It will show value of prop1=2.    We can't delete the property. It can just be modified. In addition to the functionalities of Object.seal(), The Object.freeze() method even won't allow minute changes to the existing properties of an object.  var object1 = {       prop1: 1   };  Object.freeze(object1);  object1.prop1 = 2;   delete object1.prop1;  console.log(object1)  It will show the value of prop1=1.   We can't delete any property or modify any property. 4)What is negative infinity? The negative infinity in Ja

Coding Interview Questions Set 3

Image
For previous  questions and answers please refer to   Coding Interview Questions Set 1  and Coding Interview Set 2 . Let's begin with set 3, 7)How to reverse a singly linked list? a)Simple recursive solution: function reverseList(head) {     if (!head || !head.next) {         return head;     }     var newHead = reverseList(head.next);     head.next.next = head;     head.next = null;     return newHead; } b)Iterative solution: function reverseList(head) {     var prev = null;     while (head) {         var next = head.next;         head.next = prev;         prev = head;         head = next;     }     return prev; } 8)Given a column title, return its corresponding column number. (Asked in Microsoft) Write a function that accepts a string s. Constraints: 1 <= s.length <= 7 s consists only of uppercase English letters. s is between "A" and "FXSHRXW". For example:     A -> 1     B -> 2     C -> 3     .

Css Interview Questions Set 1

Image
Let's begin with set 1, 1)What is CSS box model? The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model: Explanation: Content - The content of the box, where text and images appear. Padding - Clears an area around the content. The padding is transparent Border - A border that goes around the padding and content. Margin - Clears an area outside the border. The margin is transparent. 2)What are selectors in CSS? In CSS, selectors are patterns used to select the elements you want to style. Here are few most used selectors, a).class : Ex:.intro Selects all elements with class="intro". b).class1.class2 Ex: .name1.name2 Selects all elements with both name1 and name2 set within its class attribute. c)#id  Ex:#firstname Selects the element with id="firstname". d)element  Ex:p Selects all <p> ele

Coding Interview Questions Set 2

Image
For first 3 questions and answers please refer to   Coding Interview Questions Set 1 . Let's begin with set 2, 4)For a given binary tree find the depth/height of a binary tree. Solution : var depth= function(root) {     if(root === undefined || root===null){         return 0;     }     return Math.max(depth(root.left),depth(root.right)) + 1; }; Logic: Divide and conquer. 5)In a given array move all the 0 's to the end without disturbing order of non-zero elements.  Solution: var moveZeroes = function(nums) {          for(var i = nums.length;i--;){         if(nums[i]===0){             nums.splice(i,1)             nums.push(0);         }     } }; Logic: Remove each 0 and add 0 at the end of the array. 6)Calculate the sum of 2 integers without using + and - operator. Solution: var add = function(a, b) {     let carry;     while(b) {         carry = a & b;         a ^= b;         b = carry << 1;     }     return a; }; Logic:

Coding Interview Questions Set 1

Image
From this article, I will be sharing with you all a series of articles on coding interview questions. So please stay connected for the latest set of questions. It will be a good brainstorming exercise and will also prepare you for coding interviews and will definitely boost your confidence. So let's start, 1)Reverse of a string with only O(1) extra memory. Solution: var reverse = function(string) {     let result = ''     for(let i= string.length -1; i >= 0; i--){         result += string[i];     }     return result; }; 2)Fizz Buzz: Write a program that will accept a number n and will output number till n but for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Solution: var fizzBuzz= function(n) {     const arr=[]     for(i=1; i<=n; i++){         if(i%15===0) arr.push("FizzBuzz")         else