How to Create Flat Version of Deeply Nested Array? | Frontend Interview Questions | Devtools Tech

preview_player
Показать описание
In this video we are going to look at an interview question where we are going to implement an algorithm that will help us in creating a flat version of deeply nested arrays. While implementing the solution, we will talk about different core #JavaScript fundamentals that interviewer can ask and make or break your interview. So, check it out!

0:00 - Introduction
1:44 - Implementation Starts
3:35 - Array Access & this Keyword
4:57 - When not to use Arrow Functions!?
6:16 - Flatten Method Implementation
12:30 - Alternative Solution
15:13 - More Interview Concepts / Follow up Questions
17:36 - Outro

We hope you learn something from our videos and they help you in some way. If they do then kindly give it a thumbs up and subscribe to our channel for more amazing videos.

You can support our channel via:

**************************************************************
Devtools Tech is a YouTube channel which was started as a collaborative effort among like-minded engineers to provide high quality programming tutorials for free. We firmly believe in knowledge sharing and easy access to quality content for everyone. Hence, this channel is an effort to give back to the community and a step towards our belief -- "We rise by lifting others".

Team Members:
Yomesh Gupta

Saloni Kathuria

Puneet Ahuja

#JavaScript #Array #Frontend #Interviews #yomeshgupta **************************************************************
Рекомендации по теме
Комментарии
Автор

really helpful, Great Content !
Thanks a lot

poonamchaurasia
Автор

My solution without using nested function in flatten by utilizing prototype method defined for array.
function flatten(){
const arr=[];
this.map(x=>{
if(Array.isArray(x)) arr.push(...x.flatten());
else arr.push(x);
})
return arr;
}

Great explaination btw. Keep posting such awesome content.

vaideshshankar
Автор

sir, can u make videos to make nested comment section and folder structure using JS

PIYUSH-lzzq
Автор

Ans for the follow up question (flatten to a certain deep ) :- function flatten(deep) {
// write your code here
let output = [];

function processing(arr, deep) {
for(let i = 0; i < arr.length ; i++ ) {
const current = arr[i];
if(Array.isArray(current) && deep > 0) {
processing(current, deep - 1);

} else {
output.push(current);
}
}

}


processing(this, deep);

return output;

}
Array.prototype.flatten = flatten;

subhabratadutta
Автор

I never thought of the 2nd solution, really impressive! Great Content !

jithendra
Автор

As always, i again fall in love with your explanation, people who are missing thia channel is loosing out so much, i am eagerly waiting to connect with you yomesh bhaiya

DilipKumar-dvzd
Автор

My answer for flattening with depth:

Array.prototype.flattenDepth = function (depth) {

let flatArray = [];

for (let i = 0; i < this.length; i++) {
if (depth && Array.isArray(this[i])) {
const returned = this[i].flattenDepth(depth - 1);
flatArray = flatArray.concat(returned);
} else {
flatArray.push(this[i]);
}
}

return flatArray;
}

bhatvikrant
Автор

Thanks this was asked in one good product base company :(

krishnachaitanya
Автор

Got the same question for one online interview for Front end round

MrAagamdoshi
Автор

Hi Yomesh,
Thanks for explaining it. Can you make a detailed video on - Time ago functionality in javascript.
I mean how diff ways and in min lines in javascript we can make this functionality.

naveenkaushik
Автор

Good content brother .. I have a question here ... the last statement of the problem says it has to be invoked existing and earlier array .. so if you try to invoke the function Input.flatten() before line 78 it will throw an error since the function flatten is not assigned yet ... so we have to use IIFE on the Array.prototype.flatten = flatten right ?

ramji
Автор

You can create a repo create a readme or file for every question and we can submit our solution for that question?
How is the idea?

Abhishek-dptc
Автор

Hi Yomesh!Can you please give an example for the depth Search?

megheshshenoy
Автор

How about this as a solution? Would love to know your thoughts Yomesh and others.

Array.prototype.flatten = function () {

let flatArray = [];

for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
const returned = this[i].flatten();
flatArray = flatArray.concat(returned);
} else {
flatArray.push(this[i]);
}
}

return flatArray;
}

bhatvikrant
Автор

Or you could demostrate them your ES2019 skills and make it happen by
Array.prototype.flatten = function() {
return this.flat(Infinity)
}

coderreal
Автор

Who are others two, in your thumbnail?

GauravYadav-rvwx