Coding Interview with Dan Abramov

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

His answer about when to use Redux is pretty fascinating, honestly.

Edit:

Will also say that it's awesome for him to put himself out there and do this!

๐Ÿ‘๏ธŽ︎ 84 ๐Ÿ‘ค๏ธŽ︎ u/camouflage365 ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

My impostor syndrome temporarily subsided with the centering task. Ben Awad could hardly contain himself.

๐Ÿ‘๏ธŽ︎ 38 ๐Ÿ‘ค๏ธŽ︎ u/jftf ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

In the tree inversion he missed the terminal case, where the leafs are null.

could also do it as... but it is not space complexity equivalent as you need to store 2x the tree in memory (atleast untill GC).

const invertTree = (node) => node
    ? {
        left: invertTree(node.right),
        right: invertTree(node.left),
        value: node.value,
      }
    : null;
๐Ÿ‘๏ธŽ︎ 25 ๐Ÿ‘ค๏ธŽ︎ u/Nullberri ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

I think invertTree needs to check for null at the beginning or before recursing, otherwise it never terminates.

๐Ÿ‘๏ธŽ︎ 22 ๐Ÿ‘ค๏ธŽ︎ u/maximoburrito ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

Oh, if only I got a question as easy as let -vs- const in a programming interview...

๐Ÿ‘๏ธŽ︎ 49 ๐Ÿ‘ค๏ธŽ︎ u/fermion72 ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

Good reminder of the fact that software engineers are horrible at interviewing software engineers

๐Ÿ‘๏ธŽ︎ 12 ๐Ÿ‘ค๏ธŽ︎ u/chillermane ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

Watching him "struggle" with centering something made me feel good inside as someone that struggles to remember the syntax of a for loop sometimes.

๐Ÿ‘๏ธŽ︎ 48 ๐Ÿ‘ค๏ธŽ︎ u/chris101010 ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies

can someone clarify the bunny pattern ? tripped me up that effin bunny

๐Ÿ‘๏ธŽ︎ 5 ๐Ÿ‘ค๏ธŽ︎ u/javanerdd ๐Ÿ“…๏ธŽ︎ Dec 03 2021 ๐Ÿ—ซ︎ replies

I quickly intuited that worst case was 200 but I was wrong as to why. My first thought was that I could check each bucket twice (in a row), which after thought wouldnโ€™t have worked. Then they did all their stuff and ended up coming to 200 for a completely different reason. So I fail. :). Probably not 200 though right? 199 I think.

๐Ÿ‘๏ธŽ︎ 3 ๐Ÿ‘ค๏ธŽ︎ u/thunder-thumbs ๐Ÿ“…๏ธŽ︎ Dec 02 2021 ๐Ÿ—ซ︎ replies
Captions
all right hello everyone welcome back to another coding interview today i have a very special guest with me dan abramov dan would you like to give just a quick introduction for yourself for those of you that have no idea who is dan uh yeah thank you for having me hi i'm done and i i work on react i did not create react but i work on it uh on the react team and i co-created the redux and a few other projects in javascript ecosystem so but today i'm gonna try to get hired by ben let's go so good luck to me sweet so the format of this interview is we're gonna start with some like brain teasers slash discussions it's not really a brain teaser just like warm you up get you thinking then we're gonna jump into kind of a web dev question and then we're gonna finish with an algo question and see how far we get so my first question for you here is all right and actually we can just we're not going to need that guy is and javascript okay there is the let keyword and there's the const keyword what do each one of these do and when should i use let and when should i use const okay um so the like both both keywords let you create the binding or like a variable um but the difference is that if you basically const prevents reassignment so if you have um if you declare cons something then you can't later do something equals whatever but if you use let then you can do that and uh note that that is not to be confused with immutability because if you declare const something equals some object and then you do like object dot whatever equals blah blah that is still allowed by const so const actually doesn't prevent that um yeah yeah like that's that's fine that doesn't break anything uh and as for which one you should use like my personal opinion is like whatever like i don't think it actually matters uh well at least like if um because if you use a constant try to reassign it there will be like an error and then they can just fix it but there's a lot of people who think that it's important to use const whenever you don't reassign i don't care like whatever your opinion is uh in the project i'll follow it and personally if you're starting a new project are you somebody who uses const are you somebody that lets every single one i'm very chaotic like i would in one function i would like always use that and then like i kind of my mood changes and then i always use by default so it's it's like totally sorry is that like are you gonna fire me though personally i actually am somebody that does cons i'm like if i don't reassign it i'll constant because it's like a microscopic check but i also in the same boat if everyone wants to use let like i'll just use let like okay my second question for you is there's this lovely library that is often used with react you may know of it it's called redux when about it should i use it in my react project well if the team is already using it that seems like a reasonable reason to keep using it other than that i don't really know like um i think like well i think it's mostly a matter of convention like if you if you work with people who already know it well like or if it's like in your requirements that it has to be implemented with redux i think it kind of makes sense um if you like i kind of think of redux as the thing where you put something that you don't know where to put and i think i would kind of look for better places to put things which i guess it doesn't answer your question but i would like first like maybe like redux can be like the last option if i don't know where to put something then i put it there but first i would explore okay like is this some kind of like server data then like maybe it needs to leave in some cache that is like purposefully designed for home and server data or like if it is some kind of ui um like if you imagine like two pieces of ui that use the same data like you you probably want to think about do i want them to be in sync with each other so like if i render the same component twice do i want uh for example type in the comment in in a post component and like there's the same post component for some reason on the same page there i want what i type to appear there and like if you don't which i think you usually don't then it should be local state even if it's kind of hoisted up but in some cases you do want the data to be shared but then that usually has to do with like things that don't actually leave on your computer right like things that are on network and then i think for those there's usually like better solutions but if you don't know where to put anything where to put a piece of data and like you have you've run it out of ideas and i think like sure like you can put it there okay so if you're starting a new project today you would only use redux if you had a piece of data where you could not figure out where to put it anywhere else like the redux is the last place you're putting it it sounds like yeah and maybe i wouldn't even use it then but okay what are you using instead what should i like are you designing your own cache is there another library you like better i think it depends on the use case right like what what is the use case you're thinking of because like for something like server uh like data coming from the server which i think in a lot of cases that's what people want to use it for like i would use like a cache like uh i don't like react query or like relay or apollo or whatever people use these days uh and then for things that are kind of local like i think usually that would just be a local state maybe hoisted at the top of the app but i would think like it's some kind of local state and if it's hoisted at the top are you do you like to use context for that yeah sure okay all right i have one last small question for you there's this dangerously set inner html thing when should i use that and react it's one of those things i can stick on one of my divs my span tags and i can use this first off what is it and when can when should i use it should i ever use it yeah um so it's basically an equivalent to the inner html dom api except inner html doesn't sound scary but this one does um the purpose is well in some cases you just have some html maybe like from your content management system uh or um just like something from your database that i don't know like maybe you had a uh you just have a bunch of html in your con like in your database and you you need to display it and like you need to pump it into the page somehow uh you can't like parse it all and so on like that that would be inefficient and you know that is safe right so i think that is kind of the key thing is like the only like you basically want to make sure that all of that html is entirely safe so it's like fully sanitized so either you're the one producing it and you're sure that you're not like interpolating some user input like i don't know whatever like the user has typed uh without sanitization or uh yeah that's basically what it is and then i think uh it actually kind of sucks like in terms of um like we would probably do it a bit differently now because the way it currently works is uh is this so there's also like this super ugly property name in order to yeah so the thing is like i think actually it would maybe make more sense to do this because the thing uh it's kind of like a chain right it's the concept of like you want this thing to be uh basically you want to set up so for example i think at meta we have this kind of a lint rule that whenever there's like a match for like uh i think dangerous is that an html or underscore underscore html somebody from security will be tagged on the pr for review so that is kind of the point it's like to prevent those holes from existing but i think it would make sense to mark this thing as tainted because it could become meant from like uh that is that is where the potential vulnerability is introduced right so like if you have something like somewhere in another file and that's where this thing gets created and then there's like a bunch of abstractions in between and then eventually like this this kind of gets used like somewhere like maybe i don't know through some other functions like that is the actual place where the potential kind of hole is yeah because like you need to verify that this part is is from a trusted input rather than this part but it is what it is and do you see like would you guys ever deprecate dangerous html and add that new prop in i guess this is outside the interview question but i'm just curious i think like probably not like it's fine uh with this but i mean the the functionality itself is like essential like it has to be there uh regarding the naming like it could be better but it's probably fine so it sounds like to me that whenever you're getting html from your server you may want to use dangerously set html and you would want to just well you need to be sure that whatever you have on the server is actually safe right that that is yeah so it's for very limited use cases where you're actually confident that it is safe otherwise you can just make a mess all right we're going to be moving on to the second question i have for you this is more of a general web design question that's going to come up and pretty much any application you ever build and i have this div here and i would really like you to center it for me both vertically and horizontally on this page so it's like right smack in the middle there okay um yeah let me try that i think actually i actually learned how to do it like literally um a few months ago let me this is the hardest question webdav i'll i'll give it a try i'm not sure i'll i'll do it right but well i think the first thing i want to do is just to make sure that it's actually well no wait i have to make sure that the container what is the case so uh i want to center it in the body right so let me just check uh what the uh i'm just checking for the bodies okay awesome okay style here we'll do this side by side okay so i think i need to or do i need to give it to the parent maybe i need to give it to the parent uh like something like anything like this and maybe i need to give maybe display flex to the parent we're getting very close wait this doesn't work why doesn't this work i thought this would work um okay let me let me is there something in the console no uh inspect this okay so body is not actually okay well but why does it have the uh the okay so are you saying that it's not actually 100 the center my body is 100 oh there's a root there okay so let's do this okay so the root is definitely not and it's the body okay this is really annoying let's but this ah so the body is 100 the root is still uh if i remove this okay that that is a discloser uh so i have the root and i have this thing uh maybe i need to give this but wait i think i have to give this to the to the parent actually maybe how it works this is really hard okay let me try this no this didn't work why why doesn't this work okay so this is still not 100 percent yeah so i'll give you a hint the root is the root needs to get bigger it needs to take over the entire screen that's one way yeah yeah that's that's what i'm trying to do but i think the route is big right like if i oh no it's not right yeah why why well i mean i could just give it like like this but i mean that's yeah but this kind of but i want to see i want to understand why like wait why doesn't one have scent work i think it's like body html tag maybe oh yeah maybe yeah maybe that's there's just like one other guy up there okay okay after that yeah yeah that worked uh let me remove this wait okay what you did there too to like kind of get around it if you get rid of the html is so what dan did is he used instead of 100 for the height he used view height which gets you like 100 or 100 view height pixels i don't even know what they're called i don't know call them pixels but just take some i think it's very view yeah but the problem is like it seems bad because yeah it's like if you have multiple of those that's that's not going to work right so it's it doesn't scale yeah that makes sense nice you have now centered a div congratulations take me a while yeah all right all right dan our last algorithm question i have for you is a classic you work at facebook and now i'm curious to see if you have the skills to work at google there's a famous question they like to ask of whether you can invert a binary tree or not so what i want you to do is write a function here fill it out for me called convert tree you get a tree as a parameter and i'm going to send you an image of what it looks like to invert a tree basically i want you to take every node the left and the right node and swap them and it should happen at every single level so i want you to uh is it is is it okay to mutate it or do you want to like ideally you do it in place mutation so to give you an idea of what this tree looks like um the tree looks like it has a left object and a bright object which then has a nested left object right but it's going gonna have like what let me make sure i'm doing this correctly it should actually really be called node not to confuse you and each node has a value like five and then a left branch and a right branch and there's a whole other tree in the left and a whole nother tree in the right possibly or left and right could be null okay does that make sense uh yeah i think should i start yeah go ahead okay let me just put this picture side by side so i can see uh so i i was actually always scared of this like binary tree thing but it sounds simpler than i thought it is but maybe maybe i'm maybe i'm i'm like under thinking it or something but uh i in my i imagine i should be able to just um so we take the uh the left thing so that we know that left and we want to we want to point the uh actually let me just read them both just in case and i'll think about think about it later so i want to point the uh the right thing to the left one and i want to point the left thing to the right one and then i think i want to recurse so i want to do this and maybe this is so i i haven't actually thought about it yet but does that seem like i'm on the right track yeah you're actually just right that's actually just how you divided your tree you just blitzed through that i'm pretty sure you took like two minutes one minute i used to hold the world record of the fastest version of binary trees on youtube but i'm pretty sure you just broke my record right there yeah i'm not super well thinking about it like i'm not i'm not super familiar with like dealing with trees you know computer science way but like in react that's like everything that's literally like yeah like that's the thing you do all the time like it just recurs into the thing do you need these intermediate variables uh probably i would expect that it should be prob it should be possible to do with just one rate because like i can save the left one if i save the left one i have it and then and then i reassign the left to the right well i'm not sure um see if i save just the let me kind of save this and just sketch that here so if i save just the left one right so like let's imagine i have left a i know it's not supposed to be but it's just easier this way so this will be a um so i want what i want to produce is like left b right a so take left into now i can reassign now that left because it has been saved so i can do this so now both of them will be we'll get like no the left and now the thread they're both a and now i don't know that right equals left and i think that would give me the same result so i should be able to do it like this yeah i think that works too and then you just have to like change all right yeah yeah i guess that's kind of ugly i'll do this yep exactly right okay then you're ready for my bonus question i didn't think we were going to get to a bonus question but you're you're doing fantastic so far let me pull up my bonus question for you one is all right this is my bonus question for you so there is a hundred holes in a line and you can think of the holes as just like you know something like this and this hole is index zero this one's index one and so on all the way up to 99 right okay and there's a rabbet and it's hiding in one of these holes okay okay and you can only look at one hole at a time like you can guess i bet the rabbit is in hole index one or i think the rabbit is an index 50. right but every time you guess like let's say i guess that rabbit's in hole one every time i do that and you guess incorrectly the rabbit jumps to an adjacent hole right so an example of that is if the rabbit was in this hole here let's say this is zero one two three and you guessed two which is not right because the rabbit's not there the rabbit can either jump to a zero or two so does it rabbit always jump the rabbit always jumps oops or heat okay and the rabbit does the rabbit ever jump to the one so if i look at the if the rabbit is at two and i look at one can the rabbit jump at two or does the rabbit always jump away from no it doesn't always reply it could you could oscillate back and forth you could guess one it goes to two um or like it you could go back it can jump back and forth between the same two holes you go back and forth okay right like let's say you guess one and he's not there and then he hops to one and you get zero but he's not there anymore and then you guess one and you can go back and forth so it can't oscillate like well the bunny can just yeah the the specific thing i want to ask is so if i if the rabbit is at is at zero and i guess uh so this is this is what i guess i guess it's one but it's not one right can the rabbit do this in the next turn can the rabbit jump at one okay cool um yep and so your job is to tell me how you would find the rabbit like what sequence of guesses would you do to find the rabbit and then if you can tell me the big o of it when you're done with it we don't yeah let's just let's just get to something that yeah okay this is really this is really tricky um i guess there's probably some simple solution but i need to think a little bit okay so every time i like i hold the rabbit jumps you good solution finds the right donkey then this is interesting i can't even like well if i just go over like each hole right i think i can still miss it right because it's not if i yeah like i could yeah i could easily miss it right if you just guessed zero one two three four or five all the way up to 99 it could just hop yeah that is that is pretty annoying um okay let me i'm probably not gonna pass this one but let me just first kind of i don't like thinking about hundreds that's it's really annoying i'll think about like three uh so let's say the how would i like do this as a human so if like let's say the rabbit is here and and let's say i could make three possible guesses right i could make it like uh get us here and be okay i could guess here and be not a cake because here nothing okay and then after this the so the rabbit could jump um the rabbit well if i if i if i'm here so the problem finishes when i uh when i find the rabbit right that's that's determination like in this case i didn't find the rabbit and then the rabbit has only one option because the rabbit is at the edge so the rabbit would have to jump would have to jump here i guess so as a human i could it's pointless to check this again because i know that so what do i know i know that the rabbit can stay at one hole so rabbit has to move so so if i check uh like n i'm sure they're not it's rapid gender neutral uh they're not and uh so so it only makes sense to check and minus one and plus one but i also know i also know that that if there if they were at some like some m they can only be at m plus one or m minus one or at the edge what are those h's uh and just to clarify when you say this right you mean like the bunny's at three after that it can only be a place two and four is that right yeah yeah yeah yeah i don't i don't see an obvious way to do it uh so one okay is it okay if i take some time to think yes because like i i can either give you like a small hint if you want or if you want to take some time to like crunch in your head like either one yeah give me a bit of time just to think about it but then like maybe like in five minutes if i if i have no success at all then you give me a hint okay okay i'll keep thinking about the scenario so say the rabbit is here and i chose the wrong place so the rabbit jumps then yeah and the rabbit can oscillate for example if i just keep checking like the same spot i would miss the rabbit because like the rabbit could be somewhere else and just just stay in that area right so i kind of have to keep checking different places but it's also annoying that there is kind of no kind of memory in the system because the like the only thing that seems of consequence is like the last action so there's like nothing that permanent like it's not like if i checked if right yeah i'm not making progress yeah like if i if i checked somewhere like that doesn't mean anything so it seems like whatever the solution is it has to take advantage of well i guess like the first i guess like the first question is like is there even a way to solve it non you know like deterministically actually like is it guaranteed that i'll find the rabbit like i'm not i'm not sure if it is see when i first saw this question i thought the same thing i was like it's actually impossible to find the rabbit it's a trick question but it turns out there's actually a way that you can deterministically always find the rabbit it kind of it kind of feels you know you know what um sorry uh one thing that this reminds me of and i may be like completely kind of off uh like not going in the right direction but it reminds me of you know like in chess if you um like if you have two rooks you can kind of squeeze the the king by moving them like step you know step by step kind of to the edge and then begin can't can't escape and i feel like maybe if i do some kind of movement where i check like the next one and then go back to previous one check the next one for back to the previous one and kind of step like this i won't give the well but yeah is it is that somewhere in the right direction ish i'm gonna give you a thing to think about that helped me like start thinking in the right direction with it and that's just like let's assume the rabbit is at position 50. yeah think about where the rabbit is gonna be at t plus one and then where it's going to be at t plus two and so on of like where the possible places it could be yeah i mean if it's so if we start uh let's start like at 50 then t plus 1 is either 49 or 51 then t plus 2 is either 48 sorry 48 or 50 or 52 and so on like it just keeps kind of like deposit depending on if they move like left to right the possibilities exactly well like one thing that is certain is that it's gonna stay like if like even odd even odd i guess that that is kind of predictable whereas it's like steeper spawn all of these are gonna be odd i don't know if that helps yep odd even oscillation yeah so um how does that help though i almost feel like it would be easier to solve if i kind of knee where the rabbit started or something but because i don't have this information i have to first encounter the rabbit by chance don't they well let's say let's assume the rabbit starts on an even index how would you find the rabbit right if you knew that information like it was there yeah well i would know that i don't know that what does this even give me like i would know that if like on each step again like the rabbit would change to the odd position and then on each step which next step it would change back but i don't see how that helps me uh to know which kind of area to look at well okay one thing that i one thing that's interesting that i know is that the rabbit cat and jump further right so they they kind of have to like if i kind of keep chasing the rabbit the rabbit has to cross me well yeah i think the rabbit kind of has to i just need to make sure that as i walk i somehow step on the places that the rabbit that i'm that i kind of squeeze the rabbit in a way that they the place they would want to jump to would be the next place that i would i would check so either they jump to the place i'm going to check next or they jump away but then eventually i squeeze them to the to the edge and then they they have to do it so i think that that would be a possible solution is like to keep walking somehow like because i know if it's like even or odd i know that i can place myself so for example if the rabbit is at is at one i forgot about check zero and so if they're if they jump left i would find them if they jump right so they're they're on two and later i check and i check one next because if they're at one i find them but if they're on three i i wait but that isn't that just like checking each number one by one uh it's it's close to that yeah so like in this in this case let's like this is extra information right it's not always going to be like this but this is a slightly easier problem to solve the slightly easier problem is you would know that the rabbit is on even index so in this case how would you corner the rabbit and find the rabbit and you're guaranteed that every other so like here's like the extra bit right you know every other guess the rabbit is on an even index because it has to go even odd even odd even not even odd right yeah yeah so i guess i would uh if i know that the rabbit starts at zero then i would check one first so if i check one uh no when i check is it that would be like the next step right so the rabbit is originally for example at zero so i check one in that case i catch the rabbit like there's no other way uh but like if the rabbit is like to you right right i still check one uh the if the rabbit jumped at one then they're caught if the rabbit jumped at three then my guess is wrong but the next thing that i do is i check like two i think it just kind of well i think maybe easier for me if i actually write if i had like a test case i could i could play with it but it's annoying to do in my in my head does it help that for you to type it out i also suggest try it with um size four and the reason why i'm suggesting size four is four is an even number and so is a hundred and that lines up nicely um i'm gonna [Music] run out of time if i just like write a small test harness i i say like take as much time as you want to take like it's it's just really annoying to think in my head yeah yeah good uh i i just i don't know if this is gonna go anywhere but it's i don't like to think about things like this purely in my head um so let's say we have um okay so let's say we have um okay this and we have well actually it's easier to just say so let's say we have let's say the rabbit starts at zero and let's say that yeah that's fine so the step function is um there are basically to um i'll just write it with random i don't know that's weird uh so if um i have this thing and let's just kind of play with it um is there still like a console here yeah you can um i'll just embrace the rest it's annoying um so where is where's the console up it should be on the right side okay console yeah awesome there is a console here okay so uh the rabbit let's say okay that seems it seems legit it seems like something a rabbit would and then i i i don't know if this is gonna be helpful at all um so the thing i want to write is a so i don't know the rabbit's position i just i can only guess really right so um if so what i want to write is really a step by step what i want to keep doing is the rabbit steps and then um oh this is so really complicated so i want to have some way to figure out this is this is how i'm gonna attempt to if i guessed right and i will break out and i will say the rabbit always i i keep doing it so uh if i just start with like just using the um okay let's find out what wait what example uh oh and i should also from okay so now i can kind of see what's going on here and then here i'll say okay so this doesn't work uh so just uh which is actually interesting so uh i yeah because i'm i'm yeah this doesn't make sense because there's only uh i there's only 11 elements right so if i do it like this so this is the naive approach where i just go through them like one by one and so i think this is this is saying that the naive approach doesn't work because if i so the rabbit started at you so third position so the rabbit jump from two to one whereas i should probably log what i'm checking so i uh and then check oh yeah right that is a good point uh so i need to show it if uh i guess if them is not well uh that's okay so let's try again yeah okay so the rabbit started too so the rabbit jumped from two to three and i'm checking zero then the rabbit wait it can jump from three to three this doesn't make sense so this should really be um if position is if we're at the end we can only go back if we're at the beginning we can only go forward wait this should be paused but if the case is dependent on the random variable we go forward or back and we shouldn't move this one anymore okay let's try again so the represented to the rabbit jumped from okay when do yeah the rabbit well do i check first does the rabbit jump or is it the same thing you check if i check first there you win but yeah so this step happens afterwards so the step happens here i think yep um let me try this again so okay so you're trying to just see if the naive um algorithm works yeah yeah i want to see because so far it kind of works but let me let me just bump this to like 30 uh and see and let me start maybe at 10. so far it kind of feels like it always finds the rabbit i know this is not a this is not a super scientific method of doing it uh but i'm just trying to kind of gain the intuition for what what are the what is the case that like that doesn't work with the naive approach if there is a case that doesn't work with the naive approach um well let me go back you were saying like four is interesting um okay so let me yeah so if i started zero oh i don't know that's easy if i if i the rabbit is initially at one then i check zero the rabbit can jump so i think if the rabbit jumps back to zero my approach is not going to work so this yeah so this this actually okay i got a case that doesn't work so the rabbit starts at one i'm checking zero the rabbit jumps to two i check one the rabbit jumps to three i check two and then the rabbit kind of misses me because the rabbit jumps over back to two and i stop it duration so that doesn't work but if i check the same what if i check the same place twice i wonder if that would work better also did you have a meeting at the at six are you good to keep going just time wise no no i'm okay no i'm good to keep going yeah but um uh i don't know if i'm going in like a completely different we're a little bit going down a hole that i'm not sure that's gonna help you i can try giving you a few more things to try and intuit it the the thing i'll say yeah i think the the thing that's going to lead you down to like figuring out the best is imagine you have four squares and then try the knight you've approached of that and see what happens when rabbit is on even index versus odd index and what you'll find is the naive approach works for one of those indexes but not the other yeah because if i start with well i guess like if i start with the like basically if if the thing that i start with is kind of synced up with the thing where the rabbit starts maybe it will work but then if it doesn't work the other way around or um okay so let's say let's say that i start face rabbit starts at one can i start well no let's say that rabbit starts at two so there are there are two uh there are two even numbers in this example right like if it starts at zero and i check at zero it's it's kind of game over right but if i if the rabbit starts at three i check zero the rabbit is not there the rabbit can go to one or a three so if the rabbit goes to one and i check one that also catches the rabbit the rabbit goes to three i check one i i i haven't caught the rabbit but the rabbit can go to um rabbit go can go to two or four well notice four doesn't exist for example oh there is no four so the rabbit yeah so yeah it seems like if i started the same like evenness so if i started at zero then i think i'm gonna catch any like even uh even rabbit in the naive approach and if i started formed i think i'll find every odd rabbit yes with the naive approach so yeah and so the reason so yeah start counting at even you find even rabbit and the same thing works for odd yeah and so basically like the well it seems like the um like i think you you need to do it but i don't know if it's the most efficient way it's kind of the most inefficient way uh so what what do we mean so far uh but what we know so far but it seems like we can do a full pass or like starting with even and then if we haven't had luck we just switch the odd and do a full pass with the with the odd numbers and then by then we're kind of guaranteed to find it um but i know i don't know if it's the best solution but at least this should be uh you know this should actually give us the the rabbit no that's right that's that's how you do it and that's why like the even odd like oscillation is the key to finding this is that you have to realize the way you're actually cornering the rabbit is by figuring out if the rabbit started on an odd or even index and that's like how you're gaining progress if you will but it's kind of weird because you don't you only gain progress when you do it a single pass and then you know for sure yeah oh that's interesting right yeah and is that the best way like there's there's no better way no this is the best way at least as far as i know this is the best like best slash only way to do it because again the rabbit can just like hop back and forth which is why you have to do the pass like you have to do it right okay yeah that that is pretty interesting i i want to try it is it okay i like try it in code yeah yeah sure actually like verify that it works yeah uh so let's say uh okay so the way we do this is so we do two paths that's right so i'm gonna write two for loops and then one of them starts with um starts with zero and then it starts with one and that seems like the only change i need to make is that so basically okay so let's try to say like i have like actually 100 items and it starts like somewhere in the middle wait do i didn't something is wrong with my code i think because it doesn't oh wait this is wrong so if not found then i want to keep searching because like you found it's just okay yeah that seems like it it works i mean it's not a guarantee but yeah that's that's pretty cool it's an interesting thing but i i hate this kind of puzzles like that's yeah it doesn't look like anything i would actually get my jaw no this is definitely the type of question that you're not expecting them to get correctly you just like probe their brain to see how they respond to it because the one thing i do appreciate about this particular question out of like some of the other ones is you didn't need to know like dijkstra's algorithm off the top of your head so you can kind of like yes you have to like do these weird puzzle things in your head but theoretically you should be able to like get to yeah of some sort uh without like just like not knowing the algorithm although some people would tell you you could just intuit dijkstra's algorithm too um i don't even know what that is shortest path of a graph don't worry about it it's yeah yeah it's it's whatever weighted graph anyway but yeah i think i think you did very good like and it was interesting to see you like want to write code to actually check if it was right um which was like like if i was doing this at a real project i'd do the exact thing right like you just write the code to check yourself it was cool to see and this is this is also my hardest question like when you just let's do the entire interview it's like all right we gotta step it up a notch for dan we gotta give him a bunny problem yeah that was good that was good how'd you like the interview which question did you like and hate the most well i think this one like both liked and hated because i i think it was i guess i i i see what he appreciated about it like i definitely like that it challenges you and also like it is just common sense so it's not like you don't need to know any fancy terms to do it but it is incredibly frustrating i think right in part because i think just uh just like psychologically it feels like you're not making progress if because like usually when you think of like um going through some sort of data and like finding something that like there's usually some measure of progress that you know that if you've covered something you don't need to visit the game right and like this this one kind of defies that into it so it it was really frustrating in that sense um but but yeah it's still it was also kind of fun but um i like to well i liked how easy it was to invert the binary tree because that always sounded scary and turned out that it's like a piece of cake no i thought the same thing when i tried it and also too the binary tree and the bunny one are definitely questions where like you probably haven't even need to use that your entire software engineering career like has the bunny come for you probably not yeah yeah i'm trying to think of some problem that is similar but it kind of sounds like like if you have to do something like if you have to search for something that kind of escapes from you like something is terribly inefficient like why why isn't this designed this way you should just watch the bunny jump out of the hole and then you know where it went yeah that's what i'm saying yeah yeah cool well i would hire you based on this one you just went through the beginning very quickly i liked your thought process when you were talking about let versus const when you use redux and the dangerously set html you're able to center a div hardest problem web design so you're going to be hired for that google would hire you because you're able to invert binary tree and you even got to the bunny question we had to give you a few hints but the bunny question is honestly really hard and you are like thinking about the right way to approach it where you're trying to make some type of progress that like that's the key like thing to realize is like i have to somehow make progress to find the bunny but at the same time it's not obvious how you make progress which is the hardest part about this particular problem which you respond with so congratulations dan i would hire you um you can start next week at bin awad academy i love having you i have a few more i have a few more interviews lined up so oh okay we'll double the pay oh yeah it was really fun thanks for doing it yeah thanks for joining you
Info
Channel: Ben Awad
Views: 227,015
Rating: 4.9485373 out of 5
Keywords:
Id: XEt09iK8IXs
Channel Id: undefined
Length: 58min 19sec (3499 seconds)
Published: Thu Dec 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.