Transcript from the "Reversing a String" Lesson
>> All right, so now it's the time, I'm gonna roll up the old sleeves. Realistically, metaphorically, proverbially, whatever you want. I recommend, at this portion, I recommend, close your laptops. It's too tempting to look stuff up. In a real interview, you can't go on Google, you can't pull out your phone.
[00:00:21] I recommend a piece of paper and a pen, old school or whiteboard, if you have one. If you're watching this at your work with a bunch of people in a classroom, a whiteboard is good too. But practice what it's like to code. Just mentally, not using a computer at all.
[00:00:35] There's no linters, there's no helpers here. And I really think if you do these, you'll understand much stronger about the problems we're gonna solve here. All right, so for this challenge, I want you to create a function to reverse a string. I even gave you some, I even helpfully gave you some sample output here.
[00:00:55] I don't wanna say it's a warm up question, but it is kind of warm up question. If you had a little trouble, that's okay. We're going to get used to the coding without our computers for a bit. I of course, I'm gonna use my computer because you can read my writing if I wrote it out.
[00:01:10] And I was told I'm not allowed to use a whiteboard because my handwriting is atrocious. So I'm just gonna code it here but I'm not gonna run it. For this particular function I want it to be actually correct. A lot of the questions I'm gonna ask later, if you get in the ball park I think that's pretty good.
[00:01:27] It's enough for the interviewer to get a good concept of like they understand what you're talking about. You might be missing a bracket or a semicolon, it doesn't matter. For this one, though, I would wanna get it pretty accurate since there's many ways to do it. And I won't say there's a wrong way, but there are more efficient ways to do it than others.
[00:01:44] So for this one, I'm gonna say use a Jem tip, which is talk outs what we want. So I want a function, and I'm gonna call it reverse cuz that's what they called it. And I know that function's gonna take a string. Cool, this is a good starting place.
[00:02:07] If you ever get stuck, just talk out what you want. If they want a function that it's gonna be later invoked, you know you want a function that returns another function. And by talking that out and getting the structure really helps you mentally picture it. So I want to reverse a string, there's a few ways to do this.
[00:02:25] I'm gonna do this in the shortest way that I can think of. So I want to return a string. But I'm not going to create a new one yet. So what I'm gonna do is I'm gonna split that string. Split, what that does is it takes the string, breaks it into an array.
[00:02:44] And now I can use reverse. I know, some of you are like I reverse iterated over a loop and then I'm gonna join it back together. And that is reversing a string. Again, you could use a for loop. You could break into an array, use a for loop, and iterate over it backwards and return that.
[00:03:06] That would also work, this is essentially what we're doing here. This is just using those built in utility methods to do it for us. All right, not too bad, yes?
>> So this would be a good place to ask the interviewer a question, right? May I use reverse?
>> Yeah, yeah. Generally for these and for most interviews, unless they explicitly say don't do something. And in about two questions there will be something I ask you to code. And I'll say, don't use this method cuz it'll do it for you. But this is something I want to see how you think.
[00:03:38] Generally I think you're fine to use whatever tools you can. Cuz again, you're whiteboard coding these most of the time. All right, so that was good, I know some of you probably struggled a little bit with coding on paper. It's a little weird, but this is how a real interview would go.
[00:03:52] It's good to get those muscles going. But in general, I always say, use an array if possible. Array is one of my favorite data structures. You can do pretty much anything you wanna do with array methods.