How to Tackle a Coding Challenge
00:21 I’ve got a code challenge for you. I sent it over to you beforehand, so you could have a look at it. And the idea of this session is that you solve this little code challenge, and I ask you questions, and you walk me through the code, and hopefully our viewers learn a little bit about one thing or another.
Cool. The question I was having for you, the code challenge, is: Create a function that accepts a string. The function should return a string with each character in the original string doubled. So for example, if you put in
Martin, then it should return
01:08 So your first thoughts about this challenge? Like I said, you already got it beforehand, but once you got it, was there anything about this challenge unclear, or what were your first thoughts when you, when you saw it? So first thoughts, I guess always when I get a challenge, I have a short moment of being like, am I going to be able to do this? But this one is relatively straightforward, and then usually when I sit down to just piece it apart, you know, and stuff goes relatively easily. So this was what my first thought.
01:35 Maybe that’s also interesting to know that even if we’ve been coding for a bit, sometimes there’s a moment of just being like, confused about a question, but I think it’s relatively clear. You wrote it out quite clearly, and the instructions that I just reformatted so they fit nicely on my screen here, just give me a chance to basically go step by step here.
02:04 I think it was interesting that you just reformatted this question, right? I mean, it’s not even a question, like a challenge, because there is no question mark in the end. The instructions, right?
02:15 Because I think just like with code in general, it sometimes helps to have it formatted in a way that makes it better readable for yourself. So I think now with the different lines, it’s kind of like three instructions how you could actually start coding.
02:30 Like the first one is Create a function that accepts a string. Like okay, so is this how, how we would start now? So actually I don’t think I would start with a function even though it’s pretty explicit in here in your instructions.
02:46 Let me try it out. So what I’d start with first is I don’t really know where is the input going to come from. Right. I assume that it should be possible to handle input from separate sources, but I don’t want to deal with this at the moment.
02:59 So I’m just going to make an example string that I’m going to use for developing the code here. And let’s stick with this one, so we can also compare. So I have this example string, and then I would just continue on a script basis.
And that just starts off as an empty string. And then in here, in my
for loop, I want to then address this and fill up the variable. And so maybe that’s interesting to see, right? You know, you don’t necessarily write the first line, and you write the second line, and you write the third line. But I’m kind of following my thought process here with, okay, I need some input. And then I’m like, okay, I’ve got to do some looping, so I start with the
for loop. This is not even a valid
for loop right now, right? Because I have an error sitting down here because I didn’t do anything in there, right.
So nicer would be to write
pass as a placeholder. Then you also see that VS Code isn’t complaining that there’s an error in there. But then at this point I was like, okay, and I’m going to need a place to hold the new string that I’m creating because strings are immutable, so I can’t change the original string.
So basically the code that you’re having right now, you would just run the file and see like, if it works, if there are errors in this state. I’d open up my terminal and then I can say
python—sorry I’m in this, I’m in the folder where I created the file
If I didn’t, then we’d end this with a
SyntaxError. Yeah. I think this is sometimes at the beginning, a very good sign if a file does nothing. So sometimes at the beginning, there is an error thrown because there is a variable you are using that is undefined, or you’re importing maybe an external package that is not installed yet.
And I could do also something like put a placeholder
print() in here or print each character while I’m iterating over it. So then looking at the output is a little more interesting because then you actually see that the loop is working, and it’s addressing each character separately.
06:10 Martin now is in the middle of solving the coding challenge. But before we continue, let’s recap a little bit how he tackled the coding challenge. He started by sorting his thoughts and communicated how he understands the challenge. That’s very vital because if you’re in a coding interview, for example, it’s very good that you’re on the same page like the interviewer.
06:32 So if you misunderstand the question, the coding interviewer can clarify a few things for you. You may ask questions if it wasn’t clear. In this case, Martin understood a coding challenge completely right, and he started coding.
Become a Member to join the conversation.