Skip to main content

Posts

javascript snippets interview questions set 1

  Hey, I have just started a new set for javascript snippets  interview questions which will make your understanding of javascript more clear. It's more like testing your knowledge and then clearing things that you have doubt or if you have heard for the first time about it. So let's begin with testing and improving yourself, Q1. Which operator returns true if the two compared values are not equal?  a)<>  b)~  c)==!  d)!== Q2. How is a forEach statement different from a for a statement?  a)Only a for statement uses a callback function.  b)A for statement is generic, but a forEach statement can be used only with an array.  c)Only a forEach statement lets you specify your own iterator.  d)A forEach statement is generic, but a for statement can be used only with an array. Q3. Which statement creates a new object using the Person constructor?  a)var student = new Person();  b)var student = construct Person;  c)var student = Person();  d)var student = construct Person(); Q4. W

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:

Javascript interview questions set 2

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

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

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

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: