Mock iOS Facebook Interview 2020 - Software Engineer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] video I'm excited to be doing something a little different today as you can tell by the title we're gonna be doing a mock iOS interview that you would encounter at Facebook so before we get started with these actual two questions here I'm going to have a couple disclaimers so number one we're working in a projective sea as you can see and the reason for that is Facebook and some other companies Google and likes still use Objective C and Objective C plus plus if you work in Swift it's cool to whiteboard over there and Swift if your interviewer is okay with it the language translation is not too hard to do it's a concepción number two so kind of my background I've gone through the Facebook interview loop twice with two offered respectively I currently do not work there I work elsewhere but kind of like first-hand knowledge of what my experience was like and number three these two questions are not from my particular interview because of the non-disclosure agreements but they are interview questions that I know for a fact that Facebook has asked colleagues of mine that do still work there so with that being said let's dive right into the questions so the first question here we have given an array of numbers we want to do these two things we want to return the number of zeros in an array and we want to return the array with these zeros move to the front so first and foremost for both of these questions actually this one and this one down here we want to ask our interviewers some clarifying questions before we make any assumptions that we're going to make so the first thing that I would ask is will this array ever be empty what type of numbers are going to be in this array so are they going to be floats doubles intz and it's numbers and I would also ask if it's cool to do these two parts of this question and two different methods we write to keep our code modular and more to the point the interviewer might be looking for something in particular so these will definitely give you some bonus points on clarifying question and make sure you and they are on the same page so for the sake of this question let's assume that our inbound array an array of NS numbers they'll always be inside of that NS number and it will never be empty and we're gonna also assume that we can write two methods to take care of this so let's dive right in and write two functions that solve this problem so first and foremost we want to write a function that returns a NS number and this is gonna be number of zeros in and this is going to be an array of NS numbers and this is fairly straightforward what we want to do is we want to create a count we're going to return a NS number and it's going to be a number with that count we want to iterate over this array and every time we come across the exposition and if it's a zero we want to whoops increment our count so it's important that we test our solution now that we have one written out but I'm gonna actually solve this one as well it will test them together for the sake of time in this video not being too long the other thing to make sure you point out to your interviewer is this solution is o of n where n is the length of this array because we iterate over the entire array we need to account for the time complexity in terms of runtime something that I didn't mention because I made an assumption and something you should mention in the interview or ask rather is if this inbound array is sorted if this is sorted this solution is very very different right if this is ordered ascending we could just find all the zeros in the beginning of the array and stop iterating on this array once we come across a one or anything that's not a zero so make sure you ask all your clarifying questions so for the second part of this question we want to return an array the same array with all the zeros moved to the front so an assumption that you can clarify here is what do we do if there are no zeros do we just return the same thing or something else for the sake of this question let's assume we just return the same array so we're gonna write a function that returns an array it's gonna be an array of NS numbers it's gonna be moves zeroes to front and we're gonna actually pass in a mutable array and again that's gonna be a mutable array of NS numbers and the reason we've picked this mutable array is so we don't have to create another array in which we're gonna copy and move the zeros this is an efficiency thing that I pointed out in terms of space time you want to use it as little memory or as efficient of a solution and it's important to you point this out to your interviewer it's definitely something that they look for and it's very very important especially at a company like Facebook where scale and efficiency is important so what we want to essentially do here is let's say we have an array of something like that we're gonna basically start iterating this array from the back to the front we're also gonna have an index that starts at the front and every time we find a zero we're gonna exchange it with the index position in the front and move the front position inwards we're also gonna make sure that once the two indices collide if we have a flag here and if we have a flag here once they collide in word somewhere in the middle we need to stop iterating so we don't inversely move the zeros back the way we're gonna do this is like so we're gonna have a integer which is gonna be let's call it J it'll be the front like I mentioned we're gonna write a for loop which is gonna be int X whiskas gonna start well let's do it integer actually this is gonna be the count minus one and for some rope if we got the semicolon that's important and we're going to say while X is greater than or equal to zero and we're gonna decrement X cool so now what we're going to do in here is we're gonna check if the thing that we find that X int value because this is an array of NS numbers is zero and X is greater than J we're gonna swap the items at X and J and we're gonna increment X moving it into the array so we're gonna say array exchange objects at X and J and we're gonna say J plus plus and this is our solution so let's actually test this out so I'll recommend testing this out on a white board of course like create a mock array because we're in Xcode we can actually type up an array really fast and do it ourselves so let's say we have an array and bear with me two seconds and first we're going to test the solution of number of zeros so we're gonna say now we're gonna log out the int value we're gonna run it in our simulator here and we'll see how many zeros we have which is 3 which is indeed the correct answer and next we're gonna test out our second function which is gonna return a array and this one is going to be moves zeroes to front and we want to remember that we need to pass in a NS mutable array so we're gonna say like so and hopefully all of our zeros are at the front of this array which they are beautiful so I know I went through these questions a little fast and there's one more question we're gonna tackle but it's important that you are very concise in how you kind of portrayed these questions and solutions not too concise where the interviewer has to play a mind guessing game with you about your assumptions and your thought process it's great to verbalize it but you want to be short sweet to the point understand the efficiencies trade-offs of time space complexity and make sure you test your answer it's very very important it's something that I've seen a lot of candidates that I've interviewed myself and colleagues miss and it sometimes testing your own solution helps to identify bugs that you might have accidentally introduced and it's okay to introduce them to a degree but if you can't identify them or don't bother checking your work it's a bit of a red flag because you might do that in your actual day to day work should the company hire you so let's move on to this question which is a little more iOS C and personally I think a little more interesting so this question more or less says given a uiview and a view hierarchy we want to accomplish two things we want to return whether or not the hierarchy contains that uiview and we also want to return the super view that UI view and I guess the assumption here would be if the hierarchy contains it so similar to the first question I would clarify first and foremost the hierarchy are we gonna get a node in here somewhere or are we gonna get the root node of the hierarchy I would also clarify if what if the hierarchy does not contain the UI view do we return nil or do we return the node again the root node and for the sake of this question let's assume that we are given the root node as a start of the hierarchy and if the UI view if the hierarchy does not contain the target view well we're gonna return nil so again we're gonna tackle these questions in two different methods so let's get into it so this is a very typical recursion question but it's also paired with iOS obviously with the view hierarchy stuff so it's a good it's a good question that people like to ask which tests a bit of your domain knowledge and also your algorithm background in terms of recursion and Big O notation runtime complexity space time so what we're gonna do is we're gonna create a method that returns a bool and it's gonna take two uiview so we're gonna say view and it's gonna say node contains target which is going to be target and let's return false we need a base case for our recursion so we're gonna say if node dot sub views count sub views not count zero we can return if the node equals a target else we want to iterate over all the sub views of the node and what we want to do is let's grab our sub view first what we want to do is we want to say if our recursive call of this function returns true we want to return true and the reason we need this if check here is if we just returned this if we just take this whole thing and throw this here you'll see that we actually get a warning well right now we get an error because I decided not to delete this but more or less yeah there it is so we'll get a warning because if we return on this for loop the very first time that it runs the subsequent sub views and the subsequent runs of this for loop won't actually ever execute and the warning actually says that exactly perfectly so we want to make sure we have that if check this is a little tougher to actually test out with the example but you can definitely verbalize the solution so let's say we given this node as a target and this node is a root we're essentially conducting breadth-first search excuse me depth-first search we're going to first pick our sub views we're gonna have more we're gonna have more than 0 sub views and we're gonna iterate over each of these so first this one recursively go down here and see if we have the target we're looking for then go down this one then go down this one and eventually let's say we're looking for this we'll come across this return true because this guy doesn't have any this guy doesn't have any children in terms of sub views and we can return it so something to keep in mind is our base case and this solution takes into account that we're only checking if the two views match in V case where it doesn't have any sub views so what happens if we're looking for this so what we should actually also do is say if the node we're sorry if the sub is the target we should also return true because we want to make sure that we account for all of these sub views and not just the very last Leafs leaves on this tree which is more or less our view hierarchy so that's part one of this question part two is very similar so we can just edit this method that we've written and that's to return the super view of our target so we'll modify this to be a UI view we'll modify this to be nil we will modify this to be we're gonna get a result out of this recursive call and we want to return the super view and in our base case what we're gonna do is we're gonna say if this the node equals a target will return the targets super view which could be the node super view as well because they're the same at that point and that basically takes care of the second part of the question so let's just walk through it real fast because I edited pretty quickly we modified it to return a UI view which can which will be the super view of the target if we find it if the node doesn't have any sub views we're gonna come in here and check if the node is the target and if it is we're gonna return the target super view inversely if the node does have sub views we're gonna iterate over each of those sub views if the sub view is the noddle again return the sub views a super view because they're siblings at that point and if we don't come into this case we're gonna get a result out of recursively calling this and if we recursively call this function we're gonna basically say if this result isn't nil because we want to make sure we didn't come down here in the recursive call which means basically we found a case where the node the node contains a target we're gonna return the results superview actually I made a mistake we're not going to return the result superview because if we get a result we're you're gonna have come in here hence we already caught weird he got the super view of it so if we returned the results super view we would be going to the super views super view which is not what we want to do so we'll actually just return results and actually that was a very good kind of unplanned example of why checking your solutions is extremely important sometimes these little gotchas and these little typos especially in a whiteboard when you're not in a coding environment are very easy to make but yeah that's a good look at two common questions that Facebook will ask in iOS interview loops these are definitely questions that you should be well prepared for you should understand the runtime and space-time nuances of them there are definitely multiple approaches to them as well so understand the trade-offs of your solution check your work I hope you guys enjoyed this video this has been heavily requested by folks on this channel and friends of mine if you liked it please do leave a like it helps out the video a lot if you're new subscribe to the channel I do a lot of iOS tech interview mobile entrepreneurship stuff here in their videos and I've been posting consistently and I'm looking forward to kind of growing this channel and glad you're here for the journey so with that being said thanks for watching I'll catch you in the next video
Info
Channel: iOS Academy
Views: 13,758
Rating: undefined out of 5
Keywords: swift, ios, app development, xcode, learn, tutorial, iPhone, iPad, tableview, getting started, beginners, Apple Developer Program, make app, apps, bootcamp, app millionaire, facebook, mock, interviews, software engineer, algorithms, google, faang
Id: 5wb9Z69zzEg
Channel Id: undefined
Length: 19min 4sec (1144 seconds)
Published: Sun Jan 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.