Posts

Coding Interview questions set 6

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

Image
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

Coding Interview Questions set 5

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

Image
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

Image
  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

Image
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

Image
  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