Posts

Showing posts from 2023

interview 18-12

 /**      *     1    5s     ***    3    4s    *****    5   3s   *******   7   2s  *********  9   1s *********** 11  0s */ print star what is javascript javascript is async or synchronous promises callback async await

Moving Zeros

Image

4048

  $ npm i npm WARN cleanup Failed to remove some directories [ npm WARN cleanup   [ npm WARN cleanup     'C:\\Users\\atyagi581\\Desktop\\SelecfROMlIST\\celestial-ws\\node_modules\\react-scripts', npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'C:\Users\atyagi581\Desktop\SelecfROMlIST\celestial-ws\node_ modules\react-scripts\node_modules'] { npm WARN cleanup       errno: -4048, npm WARN cleanup       code: 'EPERM', npm WARN cleanup       syscall: 'rmdir', npm WARN cleanup       path: 'C:\\Users\\atyagi581\\Desktop\\SelecfROMlIST\\celestial-ws\\node_modules\\react-scripts\\node_mod ules' npm WARN cleanup     } npm WARN cleanup   ], npm WARN cleanup   [ npm WARN cleanup     'C:\\Users\\atyagi581\\Desktop\\SelecfROMlIST\\celestial-ws\\node_...
 let a = 20; let b=a; a=30; console.log(b); how to optimize an app how chunks of js files come if we modify only 1 out of 4, if those are cached before promise.all redux storybook code spliting

TO the new

 const arr= [1,2,3,2,3,4,5,6,6,1,3,2]; let output=arr.reduce((acc,curr)=>{   if(acc[curr]){     acc[curr]+=1;   }   else{     acc[curr]=1;   } return acc; },{}) console.log(output) // const arr =[1,2,3,4,5,6]; // const arrMap =(logic,arr)=>{ //   let output=[]; //   for(let i=0;i<=arr.length,i++){ //     let data= logic(arr[i]); //     output.push(data) //   } //   return output; // } // arrMap((element)=>{ //   element*2; // },arr) const  name='Ankit'.split(''); for(let i=name.length-1;i>=0;i--){   console.log(name[i]) }

Capgemini

  // var a= 1; // console.log(a); // var a = 2; // console.log(a); // let a= 1; // console.log(a); // let a= 2; // console.log(a); //call //apply //bind // var employee1 = { firstName: "John", lastName: "Rodson" }; // var employee2 = { firstName: "Jimmy", lastName: "Baily" };  // let invite = (greeting1, greeting2) => {  console.log(greeting1 + " " + this.firstName + " " + this.lastName + ", " + greeting2  );} // invite.call(employee1, "Hello", "How are you?"); // invite.call(employee2, "Hello", "How are you?"); const data =()=>{ console.log(this)     } data() //methods // function data(){ //     console.log(this) // } https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze Lifecycle method in react child to parent why closures iffi , hoisiting Promise.race vs promise.all vs async await https://www.geeksforgeeks.org/difference-betwee...
  exception handling const arr= [9,3,1,8,12]; /**  * find the missing Numbers without built in method  * output-->[2,4,5,6,7,10,11]  *   * Approach-->  * sort the Array [1,3,8,9,12]  * iterate and check for missingNumber  * push the missing number in a new Array */ const getSortedArr =(array)=>{   for(let i=array.length;i>0;i--){     let isSwapped ;     for(let j=0; j<i-1;j++){       if(array[j]>array[j+1]){       [array[j],array[j+1]]= [array[j+1],array[j]]        isSwapped= true;       }     }     if(!isSwapped){       break;     }   }   return array } /**  * @params number[]  * @function findMissingNumber  * return number[] */ const findMissingNumber=(arr)=>{   const sorted= new Set(getSortedArr(arr));   const getMaxLength=  [...sorted].pop(); ...

Interview

 //   function checkevenodd(num){ //     return new Promise((res,rej)=>{ //     if(num%2===0){ //       res('resolved even number') //     } //     else rej('not an evne number')      //   }) //   }    // checkevenodd(2).then((res)=>console.log(res)) // .catch((err)=>console.log(err)) // (function(){ //   setTimeout(()=> console.log(1),2000); //   console.log(2); //   setTimeout(()=> console.log(3),0); //   console.log(4); // setTimeout(()=> console.log(4),2000); //     })(); // const b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // for (let i = 0; i < 10; i++) { //   setTimeout(() => console.log(b[i]), 1000); // } // for (var i = 0; i < 10; i++) { //   setTimeout((i) => console.log(b[i]), 1000); // } const arr =[1,2,3,[[4]],5,[6],[[[[[[9],8],9]]]]] let a...
order in css inline elements position in css  z-index css grid and flex-box reverse the icon  change the order of the icons  // sayHi(); // function sayHi() { //   console.log(name); //   console.log(age); //   var name = "Lydia"; //   let age = 21; // } //myfn(); //function myfn() {   // let x = 10;   // const y = 20;   // var z = 30;   // a = 40; //} // console.log(x); // console.log(y); // console.log(z); // console.log(a); // const shape = { //   radius: 10, //   diameter() { //     return this.radius * 2; //   }, //   perimeter: () => 2 * this.radius, // }; // console.log(shape.diameter()); // console.log(shape.perimeter()); // function Person(firstName, lastName) { //   this.firstName = firstName; //   this.lastName = lastName; // } // const lydia = new Person('Lydia', 'Hallie'); // const sarah = Person('Sarah', 'Smith'); // conso...
  const arr= [1,2,3] let arr1= [...arr]; arr1.push(4); console.log(arr)  //[1,2,3] console.log(arr1) //[1,2,3,4] console.log(arr==arr1)  //false; console.log(arr===arr1) //false; print name  letter after 1 second code splitting difference between class-based components and functional component controlled uncontrolled component error boundary in case of class and functional component debouncing and throttling optimising a react app

EPAM

  https://leetcode.com/problems/longest-common-prefix/description/

Xebia Interview questions

 // const arr=[5, 7, 9, 17, 15, 11, 13, 12, 9, 15, 7, 13]; // //output==>[7,9,13...] // // for(let i=0 ;i<arr.length ;i++){    // // } // const output= arr.reduce((acc,curr)=>{    //   if(acc[curr]){ //     acc[curr] +=1; //   } //   else{ //   acc[curr]=1;      //   } //   return acc;       // },{}) // const ans=[]; // Object.entries(output).forEach(([key,value])=>{ //   if(value>1){ //   ans.push(Number(key)) // } //   }) // console.log(ans) // const arr =[11,12,41,13,71,89,90,90]; // const sortedArr =arr.sort((a,b)=>b-a); // let gratest= sortedArr[0]; // console.log(sortedArr.filter((val)=>val !==gratest)[0]) // const str="aababaabcab"; // //a6b4c1 // const output =str.split('').reduce((acc,curr)=>{ //     if(acc[curr]){ //     acc[curr] +=1;       ...

Coforge final

  //A-BC-EFG ---> GFE-CBA //Input : A-BC-EFG //G-FE-CBA const input ="A-BC-EFG" //G-FE-CBA // console.log(input.split('').reverse().join('')); //A-BC-EFG //G-FE-CBA // const reverseString =(arr)=>{ //   let postion=[] //   let reverseStr =[]; //   for(let i=arr.length-1; i>=0;i--){ //     if(arr[i] !=='-'){ //     reverseStr.push(arr[i]) //     } //     else{ //       postion.push(i)  //     } //   } //   for(let i=0; i< postion.length ;i++){    //     reverseStr.splice(postion[i],0,'-')            //   } //   return reverseStr.join('')         // } // console.log(reverseString(input.split(''))) // const arr =[1,0,2,3,0,4,11,9,7,0,15] // const ans=[]; // for(let i =0 ; i< arr.length ;i++ ){ //   if(arr[i] !==0) ...

Flatten an array Completely

 const arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]   var flat = function (arr, n) {     // return arr.flat(n)     let modifiedArr=arr;     for(let i =0 ;i <n ;i++){     let output=modifiedArr.reduce((acc,curr)=>{         if(Array.isArray(curr)){             return [...acc,...curr];         }         else{           return [...acc,curr]         }              return acc;         },[])     modifiedArr=output;     } return modifiedArr; }; console.log(flat(arr,0)) var flat = function(arr) {   let modifiedArr = [];   arr.forEach((item) => {     if (Array.isArray(item)) {       modifiedArr.push(...flat(item)); // Recursively flatten nested arrays     } else { ...

Piping Function

Image
 What is a piping function ? The result of one function will be the argument of another function. Normal code ---> const val ={ salary:10000 } const getSalary =(person)=> person.salary; const addBonus = (netSalary)=>netSalary+1000; const deductTax=(grossSalary) =>{ const percentDeduction =grossSalary * 0.3 return grossSalary - percentDeduction } const pipe = (getSalary,addBonus,deductTax)=>{ return (val)=>{ const person=val; const netSalary=getSalary(person); const grossSalary =addBonus(netSalary); const CreditSalary = deductTax(grossSalary); return CreditSalary; } } const result =pipe(getSalary,addBonus,deductTax)(val); console.log(result) PIPING CODE const val ={ salary:10000 } const getSalary =(person)=> person.salary; const addBonus = (netSalary)=>netSalary+1000; const deductTax=(grossSalary) =>grossSalary - (grossSalary * 0.3) const pipe = (...fns)=>{ return (val)=>{ fns.for...

Cube

 //caching //addition , a+b+c  add(2,3) // sum operation is a heavy operator  const sortedString=(arr)=>{   const sort = arr.sort((a,b)=>a-b)   return sort.join('').toString() } let cache ={}; const add = (...allVal)=>{   //look for the value exist or not   if(cache[sortedString(allVal)]){       console.log('from cache')     return cache[sortedString(allVal)]   }   else{     const sum = allVal.reduce((acc,curr)=>{     acc+=curr;     return acc;   },0)   cache[sortedString(allVal)]=sum;   console.log('from computation')   return sum;   } } console.log(add(2,3,4,5)) console.log(add(2,3,4,5)) // let cache={ //   '2':5,  //   '32':5 // } // console.log(cache['23']|| cache['32']) FIXED LOGIC const sortedString = (arr) => {   return arr.sort((a, b) => a - b) .join(','); }; let cache = {}; const add = (...allVal) => { ...

File Explorer

https://file-explorer-project.vercel.app/ https://github.com/ankittyagi206/explorer

Interview Questions

 // // Input- abbcccdddd // // Output- a1b2c3d4 // const input= "abbcccdddd".split('').reduce((acc,curr,index,arr)=>{ //   co //   if(acc[curr]){ //     acc[curr]+=1; //   } //   else{ //     acc[curr]=1; //   } //   return acc;    // },{}) // console.log(input) // Sum(1)(2)(3)(4)() // Output-10 // const Sum = function (val){ //   return (val1)=>{ //     return (val2)=>{ //       return (val3)=>{ //         return val+val1+val2+val3 //       } //     }   //   } // } const Sum = function (val) {   return function (val1) {     return function (val2) {       return function (val3) {         if (val1 !== undefined && val2 !== undefined && val3 !== undefined) {         ...

Interview Machine Code Round: File Explorer

  https://www.youtube.com/watch?v=20F_KzHPpvI&t=119s const arr =[0,3,0,10,0,2] //3,10,2,0,0,0 const ans=[]; for( let i=0; i< arr.length ;i++){ if(arr[i]!==0){ ans.push(arr[i]) } } if(ans.length !==arr.length){ let getremanining= arr.length -ans.length; for(let i=0;i <getremanining ;i++){ ans.push(0); } } console.log(ans)

Hoisting In Javascript

Image
https://www.youtube.com/watch?v=Fnlnw8uY6jo Hoisting --> var x= 7; function getName(){ console.log("Namaste Javascript") } getName(); console.log(x); output-->  Namaste Javascript 7 But what if we try to access them before the initialization.  getName(); console.log(x); var x= 7; function getName(){ console.log("Namaste Javascript") } Output: Namaste Javascript undefined means getName was able to find the getName and invoked the function directly but var x was not. what is this undefined?? what if I even remove the initialization of x?? getName(); console.log(x); function getName(){ console.log("Namaste Javascript") } Output: Namaste Javascript index.js:3 console.log(x); ^ ReferenceError: x is not defined at Object.<anonymous> (index.js:3:13) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/mod...

JS Interview

  https://www.youtube.com/watch?v=_2KyYAzAUOk&t=178s

Find the Highest Altitude

  https://leetcode.com/problems/find-the-highest-altitude/description/

Increment Counter

 Problem Statement=> you have to create two buttons-> Start and Stop when click on the start a counter should start from zero and it should increment every second. when click on stop this counter should pause and on clicking of the start button again resume the counter. If you do like this it will not work and will get stop just after incrementing 1; It's good you are using useRef to persist the value that you are pointing it to. why the state is not updating? SetInterval runs in a different queue and also we are not using useEffect and each and everytime it is getting it value as 0, the loop is running . Please explain why count inside setCount( count + 1 ) is always 0. As set interval runs in different queue, even after state update, it is not able to get the updated count value Why did u use a ref instead of a normal let variable? Variables will be redeclared when components re-render, that is why we either use useState or useRef to store values in React.

Basics

Image
https://leetcode.com/problems/palindrome-linked-list/description/ https://www.w3schools.com/jsref/jsref_slice_array.asp   const arr=[1,2] ; console.log(arr.reverse().join()===arr.join()); ChatGPT The expression arr.reverse() reverses the order of elements in the arr array in place, meaning it modifies the original array. The arr.join() method converts the elements of the array into a string, with each element separated by a comma by default. In the given code, arr.reverse().join() returns a string representation of the reversed array. However, the original arr array has also been reversed because the reverse() method modifies the array directly. Therefore, arr.reverse().join() === arr.join() will evaluate to true because both sides of the comparison refer to the same array that has been reversed and then converted to a string. const newarr =arr; return newarr.reverse().join()===arr.join() ; ChatGPT In the code you provided, newarr is assigned the reference to the arr ...