Interview Questions- Part-17

Guess the output - Slice doesnt affect the orginal array

				
					let arr1 = [1, 2, 3];
let arr2 = arr1;
arr1.push(4);
console.log("arr1", arr1);
console.log("arr2", arr1);
//Output
// arr1 (4) [1, 2, 3, 4]
// arr2 (4) [1, 2, 3, 4]

let arr5 = [1, 2, 3];
let arr6 = [...arr5];
arr6.push(4);
console.log("arr5", arr5); //[1,2,3]
console.log("arr6", arr6); //[1,2,3,4]

let arr8 = [1, 2, 3];
let arr9 = arr8.slice();
arr8.push(4);
console.log("arr8", arr8); //[1,2,3]
console.log("arr9", arr9); //[1,2,3,4]
				
			

Print all the sons of this family.

				
					//Print all the sons of this family...
let family = [
  {
    father: "Narasimha",
    sons: ["Rohan", "Roshan"],
  },
  {
    father: "Ganesh",
    sons: ["Anurag", "Abhishek"],
  },
  {
    father: "Rudra Prasad",
    sons: ["Ritesh", "Ritika"],
  },
  {
    father: "Rajesh Prasad",
    sons: ["Lisa", "Bablu"],
  },
];

let finalList = family.reduce((acc, curr) => {
  return [...acc, ...curr.sons];
}, []);

// ['Rohan', 'Roshan', 'Anurag', 'Abhishek', 'Ritesh', 'Ritika', 'Lisa', 'Bablu']
// --> don't miss the last [] part else you will get error..
// acc is not iterable
				
			

1 missing number between continous series of number

				
					//find the missing number between 1 to 20
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
console.log((20 * 21) / 2 - arr.reduce((acc, curr) => acc + curr));

				
			

sort array of objects by property

				
					//sort the array objects by title
var library = [
  { author: "Nehru", title: "The Discovery Of India" },
  { author: "Dr A.P.J Abdul Kalam", title: "Wings of fire" },
  { author: "Indira Gandhi", title: "My Truth" },
];

library.sort((a, b) => (a.title > b.title ? 1 : -1));
console.log(library);
				
			

Create blank array of size n (fill with zero)

				
					//3 ways to create blank array of n size
const arr = Array.from(Array(10), () => 0);
console.log(arr);
const arr1 = Array.apply(null, Array(10)).map(() => 0);
console.log(arr1);
const arr2 = new Array(10).fill(0);
console.log(arr2);

var another = Array.of(0,0,0,0,0,0,0,0,0,0);
console.log(another) 

//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
				
			

Shuffle Array randomly

				
					//shuffle array
let arr = [1, 2, 3, 4, 5];
let shuffledArray = arr
  .map((val) => ({ val, sort: Math.random() }))
  .sort((x, y) => x.sort - y.sort)
  .map(({ val }) => val);

console.log(shuffledArray);
//[3, 1, 5, 2, 4]
//[3, 2, 4, 5, 1]
				
			
				
					//Fisher yates shuffle
/*
currentIndex at start will be -5 
randomIndex will be between - 0 till 4 ?? how ?
Math.random() --say it comes as 0.9 
0.98*5 => 4.9 ..Floor will make it 4
So random Index will be always between 0 till 4
currentIndex --4 reduced
so in first iteration - 
we swap here --current index(4) and randomIndex(4)
[1,2,3,4,5] --> [1,2,3,4,5]

second iteration
currentIndex-4
randomIndex will be between - 0 till 4
let us assume we get 2 again randomIndex
currentIndex --3 reduced
we swap here --current index(3) and randomIndex(2)
[1,2,3,4,5] --> [1,2,4,3,5]

third iteration
currentIndex-3
randomIndex will be between - 0 till 4
let us assume we get 1 again randomIndex
currentIndex --2 reduced
we swap here --current index(2) and randomIndex(1)
[1,2,4,3,5] --> [1,4,2,3,5] 

fourth iteration
currentIndex-2
randomIndex will be between - 0 till 4
let us assume we get 0 again randomIndex
currentIndex --1 reduced
we swap here --current index(1) and randomIndex(0)
[1,4,2,3,5] --> [4,1,2,3,5] 


last iteration
currentIndex-1
randomIndex will be between - 0 till 4
let us assume we get 0 again randomIndex
currentIndex --0 reduced
we swap here --current index(0) and randomIndex(0)
[4,1,2,3,5] --> [4,1,2,3,5]  


*/
let shuffle = (array) => {
  let currentIndex = array.length;
  let randomIndex;

  while (currentIndex !== 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    console.log(randomIndex);

    currentIndex--;

    [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
  }
  return array;
};
console.log(shuffle([1, 2, 3, 4, 5]));
				
			

Empty Array

				
					//How to empty array
var array1 = [1, 2, 3, 4];
array1 = [];
console.log("array1", array1);

var array2 = [1, 2, 3, 4];
array2.length = 0;
console.log("array2", array2);

var array3 = [1, 2, 3, 4];
while (array3.length > 0) {
  array3.pop();
}
console.log("array3", array3);

var array4 = [1, 2, 3, 4];
array4.splice(0, array4.length);
console.log("array4", array4);

//output:- 
// array1 []
// array2 []
// array3 []
// array4 []

				
			

Leave a Comment