Posts

Coding Interview Questions set 5

Let's begin with set 5, Q11)Valid Parentheses problem. 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=[];            for(let i=0;i<s.length;i++){        if(s[i] in obj) {           if(!stack.length || stack[stack.length-1]!==obj[s[i]]) {               return false;            }           else stack.pop();      }else stack.push(s[i]);   }  return !stack.length; }; L

Data structure problem 1:Implement a queue using 2 stacks.

This is one of the interview questions asked in technical or data structure round. As we know stack is first in last out and queue is first in first out. Here we have to implement the behaviour of queue using 2 stacks. So let's begin with one of the approaches, For 2 stacks we have define 2 arrays; var stack1=[]; var stack2=[]; For insertion or push, we will use stack1. For any insertion, we can push it in stack1 array. a)Insert "A" stack1.push("A"); b)Insert "B" stack1.push("B"); c)Insert "C" stack1.push("C"); Now when we remove or pop, the first pushed element should be removed first i.e behaviour of a queue.    So if we poped from stack1 we will get the behaviour of stack here. stack1.pop()//"C" will be removed i.e behaviour of stack last in first out. We can't use this, so we will need stack2 for this purpose to implement the behaviour of the queue. d)Remove or pop: For first remove or pop, we will need

javascript snippets interview questions set 3

For the previous set please refer to  javascript snippets questions set 2. Let's  begin with set 3, Q12)After the following code, what is the value of a.length? var a = ['dog', 'cat', 'hen']; a[100] = 'fox'; a) 101 b) 3 c) 4 d) 100 Q13)What is the value of dessert.type after executing this code? const dessert = { type: 'pie' }; dessert.type = 'pudding'; a) pie b) The code will throw an error. c) pudding d) undefined Q14)Why would you include a "use strict" statement in a JavaScript file? a) to tell parsers to interpret your JavaScript syntax loosely b) to tell parsers to enforce all JavaScript syntax rules when processing your code c) to instruct the browser to automatically fix any errors it finds in the code d) to enable ES6 features in your code Q15)Which variable is an implicit parameter for every function in JavaScript? a) Arguments b) args c) argsArray d) argumentsList I hope you like the article. Please stay connect

javascript snippets interview questions set 2

For the previous set please refer to javascript snippets questions set 1. Let's  begin with set 2, Q6)What is the result of running this code? sum(10, 20); diff(10, 20); function sum(x, y) {   return x + y; } let diff = function (x, y) {   return x - y; };  a) 30, ReferenceError, 30, -10  b) 30, ReferenceError  c) 30, -10  d) ReferenceError, -10 Q7)Which of the following values is not a Boolean false?  a) Boolean(0)  b) Boolean("")  c) Boolean(NaN)  d) Boolean("false") Q8)For the following class, how do you get the value of 42 from an instance of X? class X {   get Y() {     return 42;   } } a) x.get('Y') b) x.Y c) x.Y() d) x.get().Y  Q9)What's one difference between the async and defer attributes of the HTML script tag?  a) The defer attribute can work synchronously.  b) The defer attribute works only with generators.  c) The defer attribute works only with promises.  d) The defer attribute will asynchronously load the scripts in order. Q10)What val

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: