5 Dec 2022

UI Lead Interview Questions - Protek Consulting

Q.1  Given an array of numbers as an input, write a function that returns a list of only fibonacci numbers.

Here are a few values in the fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …

The Fibonacci number at the N-th position is 

F(N) = F(N-1) + F(N-2)

F(0) = 0; F(1) = 1

Ex: Input => [1, 13, 4, 5, 34, 23, 99, 55] ,

Output => [1, 5, 13, 34, 55]


Solution:

 var inputArr = [1, 13, 4, 5, 34, 23, 99, 55];

  inputArr.sort((a, b) => a < b)

var genFab = (inputArr) =>{

     var finalArr = [];

     tempArr = [];

     var i = 0;

     var j = 1;

     var sum = 0;

     var maxVal = inputArr.sort((a, b) => b - a)[0];

     while( sum <= maxVal){

         sum =  i + j;

         i =  j;

         j =  sum;

         tempArr.push(sum);  

         if(inputArr.includes(sum)){

              finalArr.push(sum)

         }

     }   

    return  finalArr;

}

var inputArr = [1, 13, 4, 5, 34, 23, 99, 55];

inputArr.sort((a, b) => a < b)

genFab(inputArr);


Q2.  Output of the below given Program -

Program 1: 

console.log('a')

console.log('b')

setTimeout(() => {

console.log('c')

}, 1)

console.log('d')

setTimeout(() => {

console.log('e')

}, 0)

console.log('f') 

Solution 1:

a

b

d

 f

 e

​ c


Program 2: 

console.log('a')

console.log('b')

setTimeout(() => {

console.log('c')

}, 2)

console.log('d')

setTimeout(() => {

console.log('e')

}, 1)

console.log('f') 

Solution 2:

a

b

d

 f

 e

​ c










11 Sept 2022

Useful Javascript Methods | Javascript method to count the specific char in string

Program:

//method definition

function countString(str, chr){

     var count = 0;

     for(var k=0; k < str.length; k++){

         if(str.charAt(k) === chr){

             count++;

         }

    }

    return count;

}

//method call

countString("Hello World", "l");

Output:

3


To The New React JS Interview Questions | TTN React Interview Questions | Tp The New UI Interview Questions | JS Interview Questions

 Qus 1.  Given a string "Hello" & write a code to print the count of the chars available to it & create the object like this

{"H":1, "e":1, "l":2, "o":1}


Soln:  

var word = "Hello";

temp = [];

arr.forEach(function(chr){

        temp[chr] = word.split(chr).length - 1;

});

var finalObj = Object.assign({}, temp);

console.log(finalObj);


 Qus 2.  Output of the below given program -

Program:

console.log(1);

console.log(2);

setTimeout(()=>{

console.log(3);

}, 3000);

console.log(4);

console.log(5);

Output:

1

2

4

5

3


 Qus 3.  Output of the below given program & what if replaces the inner function as a arrow method -

Program: 

var personObj = {

    first_name: 'Alex',

    last_name: 'Waugh',


    getName: function(){

        console.log( this.first_name, this.last_name)

    }

}

personObj.getName();

Output:

Alex Waugh

//if replaced the getName to arrow method then what will be the output

undefined undefined


8 Aug 2022

Insertion Sort Example in JS -

function insertionSort(mn){

    var n = mn.length;

    for(var i= 1; i <= n-1; i++){

      var j = i - 1;

      var key = mn[i];

         while(j >= 0  && mn[j] > key){

              mn[j+1] = mn[j]

              j--;

         }

         mn[j+1] = key;

    }

    return mn;

};

var mn = [34, 6, 793, 3000, 6, 7, 245];

insertionSort(mn);

Output:

 [6, 6, 7, 34, 245, 793, 3000]

Binary Search Example in JS -

 

Solution :

function binarySearch(arr, val, start, end){

    let mid  =  Math.floor((end - start) / 2);

    if(arr[mid] === val){

        return mid;

    }

    if(arr[mid] > val){

        start = start;

        end = mid    

        return binarySearch(arr, val, start, end);

    }

    if(arr[mid] < val){

        start = mid;

        end = mid    

        return binarySearch(arr, val, start, end);

    }

}

var arr = [72, 3, 6, 8, 9, 25, 4, 18];

//first sorting the given array

arr..sort(function (a, b) {  return a - b;  });    OR   arr.sort((a, b) => a-b);

[3, 4, 6, 8, 9, 18, 25, 72]

var res = binarySearch(arr, 6, 0, arr.length);

console.log(res);

Output:  2







6 Aug 2022

Bubble Sorting Example in JS -

Qus:  Sort this Array  [34, 5, 8, 26, 9, 47, 62, 3];

Solution : 

var in = [34, 5, 8, 26, 9, 47, 62, 3];

for(let j =0; j < (arr.length-1); j++){

   for(let i =0; i < (arr.length-1); i++){

        if(arr[i] > arr[i+1]){

               let temp =  arr[i];

                arr[i] = arr[i+1];

                arr[i+1]  = temp;

        }

    }

}


Result :

 [3, 5, 8, 9, 26, 34, 47, 62]

28 May 2022

React JS Interview Questions

1. What is React?

Ans: React is a JavaScript library for creating user interfaces. It is open source library specifically for single page applications.

  • It is a efficient, flexible & declarative library.
  • React is component based library.
  • It is maintained by a community of developers & companies. Formerly it was maintained by Facebook.


2. What is the Lifecycle of React Component?

Ans: React Component have mainly 4 stages through which it passes

  • Initialization: It is the first step when component prepares with the available props & states
  • Mounting: It is the stage of rendering JSX which is returned by render method
  • Updating: In this stage, component gets updated due to updates of props or state
  • Unmounting:  In Unmounting state, component gets destroys & known as the final step of the Lifecycle

 

3. What is the Component in React?

Ans: Component is the smallest part of UI which can be created using react. 

  • It is reusable & Independent part of code
  • It makes the UI building easier
  • Two type of Component can be created in React i.e. Functional Component & Class Component

4. What is JSX?

Ans: JSX is an extension of the JavaScript syntax. It stands for JavaScript XML. 

  • JSX is more closer to JavaScript than HTML.
  • It allows use to write HTML in JavaScript.
  • It helps to prevent injection attacks
  • Its as statically typed & object oriented programming language

5. What is Virtual DOM in React?

Ans: Virtual DOM is a JavaScript object & Its light weight in nature. It is a programming concept & implemented in JavaScript library on top of the browser APIs.

  • Virtual DOM keeps a light weight representation of Real DOM in Memory
  • It is a virtual representation of the UI which uses ReactDOM library to be synched with the Real DOM
  • Virtual DOM is more like a pattern than concept


6. What is React Fiber?

Ans: It is new engine for reconciliation in React. Its an ongoing re-implementation of Reacts core Algorithm

  • React Fiber is going to introduce some new concepts

7. What is Reconciliation in React?

Ans:  Reconciliation is a process in React. It uses mechanism like diffing to compare two object to determine which part should be update in DOM
  • React updates the DOM through reconciliation process. Due to Reconciliation process it works faster.
  • Virtual DOM & Diffing algorithm is two important concept behind reconciliation process.

8. What is State in React?

Ans:  State is a built-in object in React. It is stored locally in React component to share it with other child component it should be passed as a props to child components.
  • Scope of State always exist in side of the its component
  • State object is initialize inside the constructor & can be used inside the component using this.state
  • It can contain a lot of properties in it. there is no limit on it 

9. What is the difference between the ES5 and ES6 standards?

Ans: There are multiple differences between ES5 and ES6.