Python Interview with a Microsoft engineer

Watch someone solve the reverse string problem in an interview with a Microsoft engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.

Interview Summary

Problem type

Reverse string

Interview question

1) Reverse the order of the words in a string 2) Modify part 1 such that the punctuation stays in place while the words are reversed.

Read more about the questions

Interview Feedback

Feedback about Swift Dinosaur (the interviewee)

Advance this person to the next round?
Thumbs up
How were their technical skills?
3/4
How was their problem solving ability?
3/4
What about their communication ability?
4/4
I think you did great for your level. If interviewing for an intern I would have def moved you onsite and passed you. You took a very simple question and did it fairly quickly (I would have preferred for you to code just a little bit faster on that). You communicated from the beginning on what you wanted to do, checked in with me and everything. The only problem I saw was forgetting something simple like .join (which should have worked, maybe we forgot a print statement or something?). Then we moved onto using punctuation and keeping it where it was originally. You did pretty well with this since this makes things much more complicated. Overall you did well. Best of luck in your future interviews.

Feedback about Indelible Raven (the interviewer)

Would you want to work with this person?
Thumbs up
How excited would you be to work with them?
2/4
How good were the questions?
2/4
How helpful was your interviewer in guiding you to the solution(s)?
3/4
1/4

Interview Transcript

Indelible Raven: Hi. You there? Can you hear me? Hey, can you hear me? Hello?
Swift Dinosaur: Hello?
Indelible Raven: Yeah, hi.
Swift Dinosaur: Hi.
Indelible Raven: That's a little better. It just kind of beeped over and over and over again, it just never went. You've used this platform before?
Swift Dinosaur: Yeah, I have.
Indelible Raven: Okay, so I don't have to explain much. Kind of what I just do differently I think, I don't know, is that I can also give you feedback at the end, like verbally and I could just tell you at the end and you can ask me questions on it. Also what is... So this is completely anonymous, so you can keep your identity secret, but a) what is your primary coding language for interviews and where you are roughly in your career, like what level generally speaking?
Swift Dinosaur: Um can you repeat the first question?
Indelible Raven: Yeah, what language do you want to use?
Swift Dinosaur: Oh, can I use Python?
Indelible Raven: Yeah, two or three?
Swift Dinosaur: Um three, I can switch it. Okay cool. Uh and then you asked what level?
Indelible Raven: Yes.
Swift Dinosaur: Um like medium to hard? Probably like a harder question.
Indelible Raven: Wait no, I mean your career level.
Swift Dinosaur: Huh?
Indelible Raven: Like your career level, like a Junior level?
Swift Dinosaur: Uhm, intern.
Indelible Raven: Okay. Right um, so I'm going to start you off with something that's hopefully going to be simple and will finish quickly. Kind of one of the rules I have is especially at these questions, but if it sounds simple, it's going to be simple, or you should think about simple. Like let's say, I want you to read in a file right? I don't want you to write the parsing function, normally just read line by line and split it, I really want you to just say read all lines, something like that. Unless I kind of mention and in this case you're probably gonna have a split function and then you're gonna do something with that and then that's not going to be terribly simple, but it'll be simple enough I hope. All I really want you to do for this one is to take an input string and reverse the words in that string. Not reverse the words themselves, but reverse the words of that string. This, is a test! will turn into test! a is This, right?
Swift Dinosaur: Okay. Yeah.
Indelible Raven: Alright, sounds good?
Swift Dinosaur: Okay. Um and then is this string given as it's... given it's a string right, like the input string?
Indelible Raven: Oh, yeah.
Swift Dinosaur: Okay, um so the first thing to do is to do like input string dot split by space?
Indelible Raven: Okay.
Swift Dinosaur: Um, I have a question. Can I like use print statements and run the code as I go?
Indelible Raven: Oh yeah.
Swift Dinosaur: Okay. Okay I'm gonna quickly run like... or like print string just to make sure it's like printing in the format or like converting to an array separated by space like I wanted to.
Indelible Raven: Yeah, feel free.
Swift Dinosaur: Okay, cool. Oh, guess that is the punctuation. Oh no, it doesn't. Okay cool. Um and then I reverse it?
Indelible Raven: What.
Swift Dinosaur: Yeah, so now that I've split it into individual words, I can do the actual reversal. Um so that's what the question is right? Like to take your input and reverse the words? Okay cool. So then reverse string stuff, start by initializing a new array and then for every element in the range, what like basically what you want to do is you want to iterate... Oh actually, I'll start with... okay so the way I'll do it is I'll start with like a left index and a right index and I'll initialize the left index to the very first element, like zero basically so the first element of the string, like the first word in the string, and then I'll initialize the right index to the last word in the string. I'll just iterate in words until like the right index is less than the left index.
Indelible Raven: Okay.
Swift Dinosaur: Um so... hmm actually... wait can I run a few more examples through you?
Indelible Raven: Go ahead.
Swift Dinosaur: Okay. So if I like... if I just have like A B C D E, this just becomes like E D C B A right?
Indelible Raven: Yeah, because each one was an individual word in a sentence.
Swift Dinosaur: So you don't actually need to do this, like left counter or right counter. You can basically like backward iterate through the original string and just append in backwards order.
Indelible Raven: I get what you are trying to do, I want to try the other way.
Swift Dinosaur: With the counters?
Indelible Raven: Yeah. Just Ctrl + Z. It's too late now, but... too late now but I'm curious why you didn't just your Ctrl + Z to undo. So I said you just kept writing over again instead of just undoing and having what you did.
Swift Dinosaur: Yeah. I'm not sure... so I'm like thinking about it for a second because... yeah so I don't need to create a new list if I'm iterating this way. This is like the constant memory way of doing it, so I can just do like temp is equal to split string of left counter and then split string of left counter equal split string of right counter and then split string of right counter equals temp.
Indelible Raven: Interesting way of swapping it. I've seen people generally just say split_string[left_counter], split_string[right_counter] = split_string[right_counter], split_string[left_counter]. That works, I'm just used to people in Python using that shortcut where they would do a, b = b, a for swapping.
Swift Dinosaur: Oh okay.
Indelible Raven: You don't need to do that, you're fine. I'm just saying that I generally speaking I've always seen Python people do it that way.
Swift Dinosaur: So I think I'm used to writing this is like this type of function in C.
Indelible Raven: Yeah I started in C as well um, so that's how I do it. Just the that people start with Python do it the other way usually.
Swift Dinosaur: Um yeah, then you can just return reverse string.
Indelible Raven: Not really?
Swift Dinosaur: Oh, split string.
Indelible Raven: No. Think about it for a second. We want to return what?
Swift Dinosaur: Oh, you want to return a string version of it.
Indelible Raven: Yeah.
Swift Dinosaur: Okay.
Indelible Raven: You remember when I said if there's something that sounds pretty simple, you should go with that? So if I want to just say convert a vector or a list of strings to a single string separated by something... there should be a simple way of doing that right?
Swift Dinosaur: You can just loop through the list and add everything?
Indelible Raven: Um okay. I take it you... Feel free to do that. As soon as I tell you, you're going to probably be like wait what, that's easiest thing ever. You know it, I'm pretty sure you know it. You're just not think about it right now. So you know what the join function is?
Swift Dinosaur: Oh yeah.
Indelible Raven: See. As soon as I told you, you would get it right away. It's just kind of a one of those that "oh right that exists" type things. Right, let's see if it works.
Swift Dinosaur: Yeah. I got a list has no attribute joins. Oh okay, I think I'm doing it incorrectly and like using it incorrectly.
Indelible Raven: Oh yeah, I know... Wait what? I got your variables mix it up for a second. Just ignore what I said.
Swift Dinosaur: Okay. This is still not returning anything.
Indelible Raven: Um so let's troubleshoot it.
Swift Dinosaur: It's something wrong with the... with the join function, I'm pretty sure. Yeah cuz it's in like the list correctly... Oh wait. That was from here though. Yeah it does not return, join does not return.
Indelible Raven: Okay um, that should work. Um I literally just looked it up. Ah I don't know, that should work. Okay we're going to go to part two. Feel free to just do a for loop.
Swift Dinosaur: Oh okay wait let's try doing it like this.
Indelible Raven: Yeah, I don't know. Um I mean let's try this, let's just use Python 2. Might have to copy and paste it in. Okay please switch to Python 2 and try it there. This is weird, because I literally just did it. I don't know. Let's pretend it actually works. Let's go to part two of this question. You notice how when we reverse 6 or 7, we reverse it from 6 to 7. We're going to think about is to leave the punctuation where it's at and still reversed the words. The exception that would be like "it's".
Swift Dinosaur: Wait can you repeat that? I'm having a really hard time, the noise is a little muffled.
Indelible Raven: Yeah I have to use my headphones right now. Usually I have a better set up, but I'm still at work. So what I want you to do is take six and you know we normally go to seven, just reverse all the words. What I want you to do is just leave the punctuation where it's at, but still reverse all the words.
Swift Dinosaur: So don't include the punctuation in the reversed version?
Indelible Raven: No, I want you to leave it where it's at. So in the reverse it would be test, a is This!. The exception being like a word like it's, obviously the apostrophe is part of "it's".
Swift Dinosaur: Okay, okay. Um okay. So in thinking about doing this, I'm not entirely sure if this is the right approach, um but it's to like... so it's to hard code all of the special punctuation characters or I guess like um...
Indelible Raven: I'm testing it out, I don't know why this would not work. Keep going, I'm listening.
Swift Dinosaur: For every like word, if that word has a character at the end of it that isn't a special character... or that is punctuation, what you can do is before you... okay so basically if that word has punctuation at the end, before you do the swap or like at some point you can just take the punctuation off of the original word and just move it to the end of the swapped word I guess and then just like swap it the same way you normally would.
Indelible Raven: What if it's before? So it's a period without a space this.
Swift Dinosaur: Um yeah like you can just iterate through the original list and it's like for every word in the input string is that word... you can convert the input string into a list and once we've done that you can iterate through the words and if the word has punctuation in it, but you can just... yeah like if the word was punctuation in it, you can just take that computation and put it in the correct spot of the word you would be swapping it with, or you swap it.
Indelible Raven: Okay. What I want you to do before you start writing code, I want to see it written in like plain English, just so we're both on the same idea here, because I'm pretty sure I know what you're saying I just want to make sure.
Swift Dinosaur: Okay um so like... so iterate through input string, find words with punctuation attached, um and then do math to find the word that you swap with, add punctuation to word you would swap with, and then add some like modified input string, reverse string function.
Indelible Raven: So 14 is not really needed right? You already have a pointer at that point right?
Swift Dinosaur: Um where? Oh 14 yeah sure.
Indelible Raven: Yeah because you already have a left and right counter, so there's no math. I assume you're going to want to remove the punctuation from the first word?
Swift Dinosaur: Oh yeah. Yeah.
Indelible Raven: So how does this handle the cases where they both have punctuation?
Swift Dinosaur: You would just swap the punctuation.
Indelible Raven: Okay. Try it out.
Swift Dinosaur: Okay um so first I'm going to create a helper function to check for punctuation. Can you have punctuation in the middle of the word as well aside from this?
Indelible Raven: We don't have time for that, I don't think. So let's just do like end.
Swift Dinosaur: Okay so just end or like front and end?
Indelible Raven: Yeah, front and end.
Swift Dinosaur: Okay. Is there a way I can import like ascii?
Indelible Raven: I have no idea. Are you saying like import the ascii?
Swift Dinosaur: I just wanted the alphabet.
Indelible Raven: Oh yeah. Like if that's a thing, then yeah feel free. Actually let's assume that it's a thing, sure. Yes sure, that works.
Swift Dinosaur: Ok. I can return like a tuple of like start end punctuation and do it all and none. So if word[0] is not in ASCII alphabet, then you want to set start to true and the punctuation to the word of 0.
Indelible Raven: Alright I think what you're looking for is just this.
Swift Dinosaur: Oh cool.
Indelible Raven: There's also the numeric, alphanumeric, stuff like that.
Swift Dinosaur: Okay.
Indelible Raven: This question could get so complicated if I added in stuff in the middle of multiple punctuations and stuff. I'm starting to realize how bad this could get. It's not going to, but...
Swift Dinosaur: Well, I mean, you can change this to index right? Instead of doing like start and end, you could just do... okay here I can just do like indexed and then this would be zero. And then here it would just be like index and then like length of word minus one. Or like, it would just be like negative one basically because you would put it at the end of the word if it's in the middle right? And then like if you have some punctuation in the middle, you can just like change this index accordingly?
Indelible Raven: Yeah that makes sense.
Swift Dinosaur: So then you just return like index and punctuation and then like you have left word, right word so then do like um left_punct. So I guess like if... okay so left... I'm gonna switch the order I return... wait okay um this is like it's like a little bit more complicated because like if you have a right word without any punctuation then you can just like do right word equals left punct plus right word but if the right word has punctuation... okay actually I think it's... yeah okay so if one is... okay we have three cases. Either both of them have punctuation or like only one of them has punctuation. So if this is not equal to none and right punct is not equal to none, then... I might be making this overly complicated but if that is the case I can just refactor like to get rid of if statements.
Indelible Raven: Just keep going like you are, it's fine.
Swift Dinosaur: Okay, um so if they're both equal to zero then um we can start by doing right word equals left punk plus right word of like 1 onwards um and then last word is equal... oh, okay yeah that's fine. And then left word is equal to right punct plus left word onwards. Oh there's two more cases... so if this is 0. I'm pretty sure there's a more efficient way of doing this.
Indelible Raven: Yeah, probably. You don't need to worry about that right now. You have like five minutes, I just want to see where you go with it.
Swift Dinosaur: Okay, um... left punctuation is at the end and right is in the beginning, you want to do... Then the last piece is if they're both negative one.
Indelible Raven: Alright. Let's stop with this, I have a good idea where I am with this. I kind of want to see you come up with some like edge cases and testing and stuff.
Swift Dinosaur: Ok. Um should I just like comment out this part then?
Indelible Raven: Uh yeah sure.
Swift Dinosaur: Ok. Um and then just like write test cases for what I currently have?
Indelible Raven: Yeah come up with test cases, stuff like that.
Swift Dinosaur: So a good way of like coming up with test cases is to figure out like every edge case. I guess like we already have, yes so you can write like one case that is like normal and doesn't ask punctuation or like none of the words have punctuation and then for like edge cases, you basically figure out like every edge case that... you can like partition into all of the edge here like cut, so I guess such that every test case covers one like edge case you have. So you could have like reverse string. So the first one could be like both words have punctuation at the end, the second is like the first one has it at the end and the second one has punctuation at the beginning, and then the first one has it at the beginning and the second one has it at the end, um and then both of them have it in the beginning um and then you would have like edge cases. I don't have code for this, but you would also have edge cases for like only one string having punctuation both at the beginning and the end and like do that for both strings and then if you would... yeah so I guess I can begin by running use and then I can spend more time in the past, I can explain like how I would have written test cases to handle punctuation in like the middle of strings.
Indelible Raven: Yeah, that makes sense. Um let's see, I'm just looking over it right now. Ah yeah um there's probably other things I would have liked to see like buffers and stuff, but I'm going to here... I'm just kind of curious to see what your thought here on this, on how you did?
Swift Dinosaur: Um, I guess like I probably wouldn't if I were to... yeah I think like if I if this wasn't an interview, like I had more time, I definitely would not have hard-coded all of these cases in. Like there's definitely a better way of doing that.
Indelible Raven: Right, but this is an interview. I kind of want you to take a look at how you did from an interviewing perspective.
Swift Dinosaur: I think I did alright. Like the swap functionality is fine, but I still think that I should have like... despite it being an interview, I should have still been able to abstract this part out, like that if the cases out better I think, and then like ideally I would have been able to test it a little bit more.
Indelible Raven: Yeah that makes sense. So you're going for an internship right?
Swift Dinosaur: Yeah.
Indelible Raven: First I should ask, you want to hear my feedback right? Okay, um first of all, I don't understand why join doesn't work. It should. It's not. So that's not your fault. I would not have counted against that and like I said, I assumed it was going to work and it should have. I kinda wish you thought about doing that. I think you knew what it was, you just didn't think about it at the time, so that's important. And then we got into punctuation, which is taking a very simple question and making it hard, as you can tell, right? It's not a very easy thing to do once you start thinking about something simple. That being said, my job here is to evaluate if you have been good to go on site, and my answer is yes, because you did really well at first, like you didn't remember join, but you mostly knew what you were doing. You made sure to clarify stuff, you wanted to check different things. When something went wrong... when you wanted to change up the style of how you wanted to do it, I kept it with swapping. You went right back to it without any problems. The reverse string with the punctuation like... it's good you came up with a punctuation function and yeah, you're right, it is a little bit messy, but what are you gonna do in 30 minutes with that? Yeah, so then... when you do an interview, you know how after a while I got pretty quiet, I was just listening? That can either be good or bad. That could be either we gave up on you and we're just letting you do whatever you want or you did pretty well so far that we just want to see where you take it and see how you take it with the different direction. And kind of let your creative work go at it, right? And that's what I was doing, I was doing part 2. It is at that point you know and you're an intern, I already said yes. I just wanted to see how you were going to implement something... it was pretty difficult in terms of punctuation in multiple spots versus just at the end, and run with it. So everything's not perfect obviously, especially since apparently join is not working here, but uh for the most part I'm pretty happy with this, I would have definitely moved you on site for sure. Yeah, also one thing I check for is I checked for your cleanliness of code: how clean it is, how readable it is and I have no problems here as well. So do you have any questions?
Swift Dinosaur: Um I don't think so.
Indelible Raven: Yeah okay, you did well. Um you took an easy problem and solved it fairly quickly. I would have liked to see a little bit quicker but then you took the harder problem and you kind of came up with a good solution and you worked with me and we talked through it... well you mostly talked to good intuition, I added a couple input, but everything again is kind of your level by the book what you should be doing in an interview, so I don't know how well you did last time, but whatever feedback they gave you is better, it got better it looks like. I guess one thing I would say is I would... I think I would expect someone out of college or an intern to know functions like is alpha or is numeric just because I know a lot of classes will do that stuff, so might be worth reviewing as well. But yeah, good job.
Swift Dinosaur: Thank you.
Indelible Raven: You don't have any other questions?
Swift Dinosaur: No.
Indelible Raven: Okay, awesome. Well best of luck in your future interviews.
Swift Dinosaur: Okay, thank you, bye.
Indelible Raven: Bye.

We know exactly what to do and say to get the company, title, and salary you want.

Interview prep and job hunting are chaos and pain. We can help. Really.