Interview Questions- Part-3

for (of vs in)

for in --> property
picks the index {0:"Rohit", 1:"Virat", 2: "Bumrah"];
for of --> value
picks the index {0:"Rohit", 1:"Virat", 2: "Bumrah"];
				
					const cricketers = ["Rohit", "Virat", "Bumrah"];
for (let name in cricketers) {
  console.log(name);
}
//!OUTPUT -0 1 2

for (let name of cricketers) {
  console.log(name);
}

//!OUTPUT -Rohit Virat Bumrah
				
			

Guess the output

				
					var x = 1,y = 1;
x;
++
y;
console.log(x);
console.log(y);

//It becomes as below
var x = 1,y = 1;
x;
++y;
console.log(x);
console.log(y);
//output :- 1 2
				
			

.every (To verify if all elements in array satisfies the condition)

				
					//Every is like all in c# - It returns value - true or false
//inline you can write or you can invoke separate function..
const ages = [10, 20, 30, 23];
const checkAge = (age) => {
  return age > 18;
};

//1st way
console.log(ages.every(checkAge));
//2nd way
console.log(
  ages.every((age) => {
    return age > 18;
  })
);

//output for both - false
				
			

Fetch value from nested array

				
					const nestedArray = [[["I am here"]]];
//Level is only 3 - so it is fine to fetch like this
//but if the level increases then we need to opt
//for other option
nestedArray.forEach((array1) => {
  array1.forEach((array2) => {
    array2.forEach((element) => {
      console.log(element);
    });
  });
});

//Fetch Recursively
//Even this might have glitch if level is too deep
const retrieveRecursively = (element) => {
  if (Array.isArray(element)) {
    return retrieveRecursively(element[0]);
  } else return element;
};
console.log(retrieveRecursively(nestedArray));

				
			

Guess the output

				
					const logNumbers = () => {
  console.log(1);
  setTimeout(() => {
    console.log(2);
  }, 1000);
  setTimeout(() => {
    console.log(3);
  }, 0);
  console.log(4);
};

logNumbers();
//output:- 1 4 3 2

				
			
				
					const logNumbers = () => {
  console.log(1);
  setTimeout(() => {
    console.log(2);
  }, 1000);
  setTimeout(() => {
    console.log(3);
  }, 1000);
  console.log(4);
};

logNumbers();
//output:- 1 4 2 3

				
			

Priority is highest for Promise than setTimeout
Output is - 1 4 5 3 2

				
					const logNumbers = () => {
  console.log(1);
  setTimeout(() => {
    console.log(2);
  }, 1000);
  setTimeout(() => {
    console.log(3);
  }, 0);

  Promise.resolve().then(() => {
    console.log(5);
  });

  console.log(4);
};

logNumbers();
//output:- 1 4 5 3 2
				
			

Guess the output

				
					let myArray = [
  { name: "Anurag", surname: "Nayak" },
  { name: "Abhishek", surname: "Nayak" },
];

console.log(myArray.indexOf({ name: "Anurag", surname: "Nayak" }));
//output :- -1
				
			

ReplaceAll string

				
					//Replace all Anurag with Abhishek
let string = "Anurag is Anurag";
console.log(string.replaceAll("Anurag", "Abhishek"));
//output:- Abhishek is Abhishek

console.log(string.replace(/Anurag/g, "Abhishek"));
//output:- Abhishek is Abhishek
				
			

Guess the output

				
					console.log(12 > 14 > 16); //false
console.log(12 < 14 < 16); //true
console.log(2 + "2"); //22
console.log(2 - "2"); //0
				
			

Flatten Array and Sum

				
					/////EXPLANATION OF FLATTENING///////
//[1, 2, [3, 4], 5, 6, [7, 8, 9]];
//1,2,[3,4],5,6,[7,8,9] --> Total input length -->6
//input.shift() -->removes first element...1 is removed
//1 is not array --> pushed to result
//2 is not array --> pushed to result ..2 is removed from input
//[3,4] is removed from input ...it is array
//[3,4] is flattened --> It is pushed to input not result
//now input is [5,6,[7,8,9],3,4]
//now 5 is straight forward not array -->pushed to result..removed from input
//now 6 is straight forward not array -->pushed to result..removed from input
//result is [1,2,5,6]
//now input is [[7,8,9],3,4]
//now [7,8,9] is flattened and pushed to input at the end
//now input is [3,4,7,8,9]
//now result is pushed with above value [1,2,5,6] + [3,4,7,8,9]
//input is empty at the end ..while loop ends
//final output ==> [1,2,5,6,3,4,7,8,9]

////////////////////////QUESTION////////////////
let input = [1, 2, [3, 4], 5, 6, [7, 8, 9]];
//expected output - sum of all elements in above array
// 1+2+3+4+5+6+7+8+9 = 45

const flatten = (input) => {
  let result = [];
  while (input.length) {
    let next = input.shift();
    if (Array.isArray(next)) {
      input.push(...next);
    } else {
      result.push(next);
    }
  }
  return result;
};

let flattenedArray = flatten(input);
let sum = flattenedArray.reduce((acc, curr) => acc + curr, 0);
console.log(flattenedArray); //[1,2,5,6,3,4,7,8,9]
console.log(sum); //45
				
			

Flatten Array -2nd way

				
					//Flatten it as [1,2,3,4,5,6,7,8,9]
//Explanation
//first it breaks into 4 items
//[[[1, 2, 3]]] -->[4] -->[5, 6, 7] -->[[8, 9]]
//Recursion runs on [[[1, 2, 3]]] till it goes and adds in else block 1,2,3
//Then it pushes to flat.push(...flattenArray(item));

//Recursion runs on [4] till it goes and adds in else block 4.
//here flat array will have only one item --> 4 in else block
//Then it pushes to flat.push(...flattenArray(item)); --existing 1,2,3 + now 4 from above statement

//Recursion runs on [5, 6, 7] till it goes and adds in else block 4.
//here flat array will have only 3 items --> 5,6,7 in else block
//Then it pushes to flat.push(...flattenArray(item));
//--existing 1,2,3,4 + now 5,6,7 from above statement

//Recursion runs on [7, 8, 9] till it goes and adds in else block 4.
//here flat array will have only 3 items --> 7,8,9 in else block
//Then it pushes to flat.push(...flattenArray(item));
//--existing 1,2,3,4,5,6 + now 7,8,9 from above statement
//--> Final Array ==> [1,2,3,4,5,6,7,8,9]

var nestedArray = [[[[1, 2, 3]]], [4], [5, 6, 7], [[8, 9]]];

const flattenArray = (items) => {
  const flat = [];
  items.forEach((item) => {
    if (Array.isArray(item)) {
      flat.push(...flattenArray(item));
      console.log(flat);
    } else {
      flat.push(item);
    }
  });
  return flat;
};

console.log(flattenArray(nestedArray));
				
			

Find the maximum in array

The recommended one is to use reduce
As for larger arrays Math.Max gives max stack overflow exception

				
					let array = [10, 20, 50, 15, 12];

//*******1***********
let max = array.reduce((max, curr) => {
  if (curr > max) {
    max = curr;
  }
  return max;
});

//*******2***********
let max_of_array1 = Math.max.apply(Math, array);

//*******3***********
let max_of_array2 = Math.max(...array);

//*******Outuput**********
console.log(max_of_array1);
console.log(max_of_array2);
console.log(max);
				
			

Leave a Comment