Loops and Hash Maps Job Preparation Interview Question

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everybody and welcome back to the channel I certainly hope you're having a fantastic morning it's been quite a while since I last put up a video onto this channel so definitely apologize for making you guys wait but in today's lesson now I'd like to present you with a very classical computer science algorithm question and this question is really interesting because it does a really good job at testing you're looping skills as well as your hashmap skills and these two skills are really important especially for a job interview all right so with that being said let me present you with the question inside of Swift playgrounds on the left side let me just rank myself down to the corner in a little bit just so you can see what the heck is going on all right so let's get started online of three right here we see a variable called sample string and the string is a 1 3 37 hopefully some of you guys get this reference on the left side you can hit the play button and you'll see a 1 33 7 inside of these quotes which represents a string now this string are here called sample string I want you to write a function that converts this strings a string of type string we want to convert this string into an integer type like that if you write this code and you wait for a little bit you'll see this red error here that says we're missing a return function so let me just fix this really quickly by returning a 0 value like that now down below I want to invoke this function using our sample string up here of 1 337 let me just pass that in as the parameter you can run this again on the right side now you'll get the wonderful value of 0 so 0 there and obviously this is the wrong answer instead of getting 0 I would like to return a value of a 1 3 3 7 instead so the weights you do this very very quickly is to use the conversion of string to integer so let's use into like that and wrap this using a string like so and pass in the string like that so I'll just remove the return of 0 now and we get the integer value here and so I don't think I need this string so let me just remove that and now you'll have the conversion of your string to an integer like that you can try to run this again but you can't exactly do this because the industry conversion here will give you an optional integer back so let's apply some nil coalescing with the double question marks right here and the default value if you can't convert your string to an integer well just default it to zero like that and once you have that little bit of code you can convert your string of 137 to your raw integer just like that right you don't see a quotes anymore and that means you have your integer 137 now if you put in ABC in front of samples string and try to run your code again you'll get the value of 0 because the conversion of this to an integer doesn't exactly work and you'll get a nil here hence returning your default value of zero that's how the nil coalescing works all right so what you're going to be asked instead of a job interview is to solve this problem without the string too so string to end conversion code and so I'll let you think about how to actually do this right now for a couple of minutes here and what I would like to do is to remove the ABC from this string and I'll show you how to handle incorrect strings in a little bit here but first what I like to do is to break this problem down into a very simple algorithm that I'll run right now instead of my function the first thing I'll do is to declare a total variable set this guy equal to zero return this inside of our function let's see return total like that and now what you can do is run this conversion function and you'll get the value of zero hopefully so that's what we get zero on the right side and that looks pretty darn good and so what I'm going to do is apply a simple so simple algorithm to solve our challenge all right so again and the old point of this question is to first test your skills on the C for loops and then the second thing is to test your hash map and your standing alright so hopefully by the end of today's lesson you guys will have a better understanding of both of these topics here and now what I'm going to do is to actually show you how I'm going to walk through the solution one step at a time here if you look at this string 133 seven you can actually break this down into let's see a series of calculations right so let's see one three three seven is actually equal to one thousand plus 300 plus 30 plus the value of seven right so 1337 if you break this down even further one through seven is equal to one times ten to the third power and so one times ten to the third plus three times ten to the second plus three times 10 so 10 to the first year and then finally you multiply let's see seven times 10 so 10 to the 0th part is what that is saying right here for those of you guys that don't know 10 to the 0 is actually a value of 1 giving you 7 times 1 at the very end here all right so that's pretty awesome and now that you understand how I'm going to solve this problem using this little trick here and the next question is how exactly do I actually get this 1 3 3 & 7 from the actual string right well the quickest way of doing that is to use sample string or maybe use this string here so let's take the string there and you can execute a for each loop on it you can hit enter one more time you'll get each of these characters FC and you can print the C value down here now running of this again down in the concept below you should see a 1 a 3 3 7 so that's pretty good but one problem or one disadvantaged rather to this for each loop here is that you don't really get the index of each of these characters and I really want to grab that the index let's say the one so that I can figure out these exponent values of three two one and zero so let me show you how to do a better version of this loop or not exactly better but better for this algorithm problem I'm going to use a four and let's say I for index and then C for the actual character here and let me iterate on let's see in string dot enumerate it like that so string is this guy enumeration will provide us with the index as well as the character inside of this loop here and let me show you exactly what this is doing and by printing out the I and C I'll remove the earlier loop because I don't really want to see it anymore and now the conversion will show two different values inside of our console let's take a look down here we have the zero index corresponds to the one and then let's see index one is three and then so on and so forth so two is three and three is seven all right now the next thing we have to figure out is how we can actually grab ahold of these exponents of three two one and zero using the I somehow and I'll show you how to do that with a variable called exponent so let exponent equal string count and I'll just subtract the value of I I think so let's see I is that let me print out what exponent is now so I print out exponent and I run this baby one more time so now down below you'll see that exponent is four three two and one and it's very close to what we need right three two one and zero this right here is just larger than the actual value that we want by the value of one so I'll just subtract it and running that again and we'll get the value of three two one and zero so three two one and zero and now all that's left is to actually multiply these characters of one three seven by our exponent and ten somehow and so the thing about this problem is you really have to declare some kind of hash map that allows you to map from your characters to some kind of integer value what I mean is I want to declare this I'm going to hash map up here and a hash map is basically a dictionary so hash map is the general term for a dictionary that you'll find in other languages so make sure you understand that these two guys are very similar if not the same now for your a value map here I'm going to declare these string or the character to the value let's say integer one and then hit a comment we'll do this for the three and three and then we'll also say seven like that so seven all right you can hit a comment at the very end if you want to do that and now what you can do down here is that for the iterations of your loop of I and C you can grab the hold of exactly what C corresponds to so let's say C is one right you can simply use this right here so let value equals the value map so value map and you can use the value of C in here and this shouldn't get you your value of one or three or seven and based on what C is but you can see this error right here it says that you can't access this map using this type of string and int because this C right here is actually a character so if you type in C look at your autocomplete is actually a character one simple way of fixing this is to use a string two character conversion so you can do this if you wanted to but there's actually easy you're fixed here so the easier fix is to make sure your keys for your value map is actually a character like that and once you make that change you can just use the values see you there you don't really have to apply this to all of the keys inside of your map because once you declare for the first one it's automatically going to infer the rest of these guys and characters and so right here is your value and if you type in the value right here so value is actually optional and you don't really want to deal with these optional intz so we're going to use an if let optional binding here just to grab the value so now our value is a non-optional int like so all right so this is the value that corresponds to the actual character from our map here and all I have to do is is a total equals value times 10 and then we need to use the integer exponent of let's see the exponent value like so right we would like to use this calculation over here and eventually a total will be 1 3 3 7 based on this string up there but if you try to run your code right now you can't exactly run this right here and you'll see that you get a value of 70 okay so with all of our calculations are written out instead of our for loop here you'll see that our conversion produces a value of 70 which obviously is incorrect right you might want to fix the total calculation here with a plus equal instead of just equal let's run this guy again and you'll see that we have a value of a 1 2 3 8 which again is not correct I'm not exactly sure what this index here even does if you wrap this guy into an exponent like that you can see your calculation changes by just a little bit here and then we get 1 to 36 all right so let me use a different type of calculation so I'm going to say let enum equals all this on the right side here so paste that in here and then I'll change this to numb and we can print out what num is along the way with our iteration on the loop let me just run that to see exactly what's going on and we have 9 24 33 and 70 so really strange there let me show you the correct solution to all this and I'm say num equals value times sed pal using this power calculator than here the X is going to be 10 and then the Y is going to be our exponent like that so this is how you calculate a 10 to the exponent right there and the last thing we have to do is to apply some conversion you'll see this error on the right says that you can't use this calculation because this LifeSite is an integer this isn't right side over here at the PAL is a decimal so this is the tedious part of converting your code it's you actually match with the types the num is going to be that and I'll just say total equals and as decimal number provide it with an actual decimal from above your decimal is the num and then you can say and value for this entire calculation here and now you can run your code again you'll see that you have the value of seven now you want to use a plus for the total and this should fix it for us we now get the value of one 337 okay so I think that is the final implementation I want to show you for today's lesson let me go ahead and get rid of these comments there and there and one final thing that you might also want to do is to check for these strings here and make sure they are valid so let me put a 2 in front of these one and I'll run my code again so convert you'll see that the conversion doesn't work because we don't have our 2 here so - you'll have to fix your value map to include all of the digits from 0 to 9 let me just pop it into here and I convert that one more time you'll have the two 137 if you put ABC here it's going to give you something strange so I run that one more time you'll get 2 1 2 1 3 37 again but if you pop this in here ABC you'll get the incorrect value and so instead you get to 1 through 37 0 0 0 and what you have to do to fix this problem is to modify the signature of your function to be optional in - like that and now this conversion you can return a nil value somewhere down here so instead of my loop if we encounter the character of something that's not inside of our map we're just going to return nil so hit the e run there and you'll see that that provides us with a nil conversion already everybody that's going to wrap it up for today's video hopefully you learned something new in today's lesson if you're interested in more about slip development and make sure to check out the latest course which is available in the description below inside of this course we learned how to build out a tinder style application where we can swipe these cards that are right swipe it to the left we can swipe it all the way to the right like that if you hit on this light button and we'll see this like animation appear just like so all right so we're building this entire application using a lot of UI kit animations as well as some firebase SDKs that allow us to login to firebase using auth and store information using a firestorm so hopefully you're interested in that course and hopefully you check that out that's gonna be it for me today I will see you in the very next video bye guys
Info
Channel: Lets Build That App
Views: 18,116
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code
Id: 2QRZqMM7ef0
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Tue Nov 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.