Python Interview with a Square engineer

Watch someone solve the threaded order of execution problem in an interview with a Square engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.

Interview Summary

Problem type

Threaded order of execution

Interview question

Given a list of numbers representing the time to sleep, write a function to output the order when each thread sleeps for the given amount of time.

Interview Feedback

Feedback about Dystopian Sphinx (the interviewee)

Advance this person to the next round?
Thumbs down
How were their technical skills?
1/4
How was their problem solving ability?
1/4
What about their communication ability?
3/4
(Same feedback as earlier - be specific about what you need clarification on. - how you look for documentation is part of the interview, in effect how do you unblock yourself. - walk through what you're doing, e.g. looking up documentation, what are you searching for, etc. Otherwise hard for me to provide guidance. - break problems down. Starting with interfaces is a good idea. Nonetheless, it's good to practice what you do if you feel stuck!

Feedback about Eponymous Squirrel (the interviewer)

Would you want to work with this person?
Thumbs up
How excited would you be to work with them?
3/4
How good were the questions?
4/4
How helpful was your interviewer in guiding you to the solution(s)?
3/4
I appreciated your patience as I struggled with a more foreign concept, and I think I learned how to better respond to working outside my comfort zone. I froze up today, but I learned three important things: 1) have a plan for unfamiliar concepts, 2) communicate what you don't know (I need to keep working at this), and 3) learn the basic implementations for the data structures that I'm not very familiar with yet in Python (in order to focus more on the logic/nature of the problem). Big thanks!

Interview Transcript

Dystopian Sphinx: Hello.
Eponymous Squirrel: Hi. Can you hear me?
Dystopian Sphinx: Yeah, can you hear me?
Eponymous Squirrel: Yeah, it's wonderful. How's your day going?
Dystopian Sphinx: Pretty good. Yeah, thanks for meeting me, you know at 10 PM or are you on the West Coast?
Eponymous Squirrel: Yeah. It's 7 PM here. So you're on the East Coast?
Dystopian Sphinx: Yeah.
Eponymous Squirrel: So before starting, tell me a little bit about yourself.
Dystopian Sphinx: Definitely yeah. I'm a junior in computer science, so I have one more summer, then I graduate. I've had some experience interning in cyber security and some experience concerning infrastructure engineering. Right, now my focus is developing my data analytics skills through Python. And yeah, I'm excited to be doing this, you know, the practice interview in this program.
Eponymous Squirrel: Awesome, what's your motivation for doing this?
Dystopian Sphinx: Yeah, I think sometimes there's like some unnecessary kind of like worry or anxiety over these interviews and I think the more... I've done these a couple times or a few times here and then you know, just in real life interviews and the more I've gone through them, just like anything else, you start getting more comfortable with them. It's a little bit easier to kind of you know recognize patterns in the problems. So yeah, I just like to practice and I feel like I'm getting a lot more comfortable with it, or I'm starting to, so this is overall why I'm doing it.
Eponymous Squirrel: Alright awesome. So yeah a little bit about me. So I'm Nelson, I currently work at Square. I've been with them for three and a half years. I've been in the Bay Area for about six years. Yeah, I'm doing this because in general, I enjoy doing interviews and it's nice to be able to help others.
Dystopian Sphinx: Definitely. You know Square is a big name right now, so that's really cool. It's really interesting, how do you like it there?
Eponymous Squirrel: It's pretty great. So I worked most of those three and a half years I've been on a team called Point of Sales that deals with the apps that you might be familiar with for taking credit card payments. But recently I've moved over to the caviar team, which is like food delivery. So, it's pretty good. Yeah, let me paste in a question here and let's get started. Read through it, let me know what you think.
Dystopian Sphinx: Cool. What do you mean when you say sleep for the introduced value?
Eponymous Squirrel: So that's like whatever sleep method you're desired programming language has. So basically like wait for that amount inside of that thread.
Dystopian Sphinx: Wait, until you get the introduced value?
Eponymous Squirrel: No, no, so for example, if we have let's say 3 1 2. So we will get a new thread for each one of them, right? So here's one thread, here's another one, there's another one. So one of them would be sleeping for three. And sleep for one. Sleep for two. And so basically it's like for this amount of time, it's not doing anything.
Dystopian Sphinx: Right. So, I guess my question is like like why are we running different threads? And what's what's the purpose of sleeping through the introduced value? I'm not exactly following.
Eponymous Squirrel: So, do you understand what what the sleep would do?
Dystopian Sphinx: No, no.
Eponymous Squirrel: So if let's say that we start these three threads at the same time. So, are you familiar with threading?
Dystopian Sphinx: Yeah, like like there can be multiple threads within a process.
Eponymous Squirrel: Yeah, so that means that all three threads are running at the same time, around the same time. And so, let's say when we say this, it's like, so this thread is is gonna pause for three seconds. This one's gonna pause for one second. This one's gonna pause for two. And then they all like push that number into a result somewhere. And so given how long they sleep for, then which one's gonna wake up first?
Dystopian Sphinx: Okay, cool. So because of you know, you're waiting for longer than you'll have the ordered list. Gotcha, right?
Eponymous Squirrel: Yep.
Dystopian Sphinx: So I guess the purpose is to implement this thread functionality. So you said like I might have to leverage you know, like Google or Stack Overflow because I'm not necessarily like experienced or very familiar with implementing threads in Python.
Eponymous Squirrel: Go ahead.
Dystopian Sphinx: So, let's see. I'm just looking up kind of primitive so I can start implementing. So for this like any other problem we wouldn't be using like any libraries right, this would all be like the standard Python library? There's no multithreading libraries, right?
Eponymous Squirrel: No. Python should have a standard library that deals with threading.
Dystopian Sphinx: Okay, cool. You know, so... I think I don't know how to approach this, so I think this is gonna be a difficult interview for me.
Eponymous Squirrel: So what kind of problems are you having?
Dystopian Sphinx: Kind of like, I mean just to be completely honest, just kind of a lack of experience working with threads in Python, I haven't worked with threads since I worked with them in C. I mean that's not excuse, it kind of gives me like feedback that I should look into like all kind of paths of the standard python library.
Eponymous Squirrel: Well I would actually say that you can't guarantee that you're not gonna see something that you're not familiar with in an interview and you should have a process for how you deal with that. So, for example right now, what type of documentation are you searching for?
Dystopian Sphinx: I'm in like a like the threading interface of Python.
Eponymous Squirrel: Okay and so what what about that is confusing or are you having trouble grappling with?
Dystopian Sphinx: Yeah like, I'm trying to figure out how to kind... as the problem states, create a thread and then have that thread sleep. Like I guess that's the first step, like I can't even think about like pushing the result because... yeah.
Eponymous Squirrel: Alright, so let's not get there then, but let's try creating threads, let's start there. How would you create a thread?
Dystopian Sphinx: Yeah, I mean, I'm being completely honest with you...
Eponymous Squirrel: I mean if you search for like Stack Overflow Python 3 create a thread, like that should give you an example, right? Look at this point you you should be able to find a just a code snippet that you can copy over and play with.
Dystopian Sphinx: Yeah so I'm looking, a lot of the... maybe I'm not searching correctly, but a lot of the thread things that pop up are tied to libraries, like multithreading and multiprocessing, so I'm trying to look at posts that don't leverage those libraries right now.
Eponymous Squirrel: So right when you search for Stack Overflow Python3 create a thread, the very first thing just uses the standard library, it's not using anything out of it, so the example that I found was how did you multiply here... well doesn't matter. So something like that. So doing this, you sort of get an idea how to create a thread.
Dystopian Sphinx: Hmm yeah, so it's kind of like this thread class and then yeah, these methods start join.
Eponymous Squirrel: So if I give you a list of numbers, how would you create a thread for each one of them? So what are you thinking, what are you pondering?
Dystopian Sphinx: I'm just have a hard time with this. I don't know if I'd be more productive to just kind of study threads. I mean, this is this is kind of one of the difficult parts about like the coding interviews for me. If it's like something I'm very familiar with, it's a lot easier for me to kind of just jump off and kind of implement the algorithm and iterate through with it, but it's a concept like this that's kind of foreign to me, it kind of stuns me, which is like, I guess is what it's doing now.
Eponymous Squirrel: Yeah, and so I think what I'm trying to guide you through is how do you deal with problems where you're not familiar with... like if someone asks you to write a compiler, like where would you start that type of thing? Because even let's say that you don't know how to create a thread right? You do know how to iterate over a list I imagine. You would be like: well like you would just share like, okay, I don't know much about threads, but I know that if I will be implementing this and I would have some sort of method that would take in the list here, for example. Then inside this list then how do you do list comprehension in Python?
Dystopian Sphinx: Something like I'm typing. x for x in list where condition?
Eponymous Squirrel: Yeah, so in this case, we won't have a condition, but like yeah, you can say like for n in numbers here, right? And then here you can have a method that's like make thread and it passes in a number. And like even if you don't know what make threads is gonna do, you're still showing that you have a ability to break down problems. So like at this point, I still don't know anything, right? According to the description of the algorithm then we also need a result somewhere. So then there's the thread. And so at this point like at least you're breaking down the problem to the point where all you need to do is understand how to create a thread. So then you go to Stack Overflow and you'd like notice like okay, so here apparently they're creating a thread this way. And so I see that it has like a threaded function and an arg and like you need to dive into what that means but at least like this way you're making progress instead of feeling like stunned and and feeling like you don't have anywhere to go.
Dystopian Sphinx: Right. Yeah, sorry, sorry about that. I had a loss of connection but um yeah.
Eponymous Squirrel: So I guess my point here is just that you don't need to be intimately familiar with everything single computer science term. The interviewers will usually be more than happy to help you through that, but you do still need to show the ability to break down problems or to make some progress.
Dystopian Sphinx: Yeah, I'm really sorry. I kind of was surprised and I did exactly what I feel like I shouldn't do in an actual interview, right? I kind of just got lost in the documentation and froze rather than...
Eponymous Squirrel: So I'm just giving you feedback now. You're doing the right thing by practicing this and getting used to... like now you know, what happens when you're faced with something you're not familiar with and you can work on that.
Dystopian Sphinx: Right cool let's see. So if we're going to like make this function, is that where you like, is that where you have a parameter there to see how long it would sleep or would that all depend on... I guess it would depend on...
Eponymous Squirrel: Yeah so n here is like amount to sleep right?
Dystopian Sphinx: Yeah cool cool. So a question on threads, if I was calling a function, could I could create the function to like return only after the thread had slept for some time. So kind of what I'm thinking is yeah, let's make this function. You start this thread, you join it. You sleep for n seconds and then after those n seconds are over, below we have this in the description, it's like this result dot push, but I feel like this function isn't really pushing anything, it's actually just like returning that number after some time and then it sleeps.
Eponymous Squirrel: Yeah, so if you do it this way though it's... The thing with threading is it needs to add the result at the right time. And so, so if we continue this so here in this thread we have this thread function, which is what does anything at all? Right, now it's just taking a number. So we're saying that here we want to sleep for n and then we want to push this into somewhere, right? But we don't have results so we probably want to pass that in as well. And so here we put results and here we put the thread to sleep. And so if you run that...
Dystopian Sphinx: Also, just a question on this line 26... Why can't we assume numbers is already given to us in the list?
Eponymous Squirrel: Sorry, say that again.
Dystopian Sphinx: Why can't we assume numbers is already in a list for us? Because we're doing the whole list comprehension like for n in numbers.
Eponymous Squirrel: Well, sorry, it wasn't list comprehension. What I meant is I just wanted to loop through. So like for n in numbers is really what I wanted to do.
Dystopian Sphinx: Cool.
Eponymous Squirrel: So if we run this now... I guess we need to actually run it. Oh, do you know what's wrong here? Can you not pass the list like this?
Dystopian Sphinx: Oh, um, inPython3, they did this like weird thing where you have to have parentheses for print.
Eponymous Squirrel: So here we're not passing the result here, so let's pass it there. And then list objects, that's to be pushed. Oh, so here in line six, it's not push, what's the method for appending to list.
Dystopian Sphinx: I think it should be append.
Eponymous Squirrel: Okay. Okay, so looks like we've printed the same order. So at this point you just start debugging right, so like we can like put here print, you can do this, right? Like that, with a plus. Maybe I can't print that. Can you not just add to a string?
Dystopian Sphinx: I guess it's percent. Should be.
Eponymous Squirrel: Oh I see. Close enough. So can you debug this? Does this give you an idea of what's wrong?
Dystopian Sphinx: Let me take a look... I think it's a logical issue because the second thing will work if they were all starting at the same time, but because we're iterating through, we're not necessarily starting them all at the same time. So for example like if if the first one's going for three seconds in the second one, let's say was going for two seconds and it starts a second later that they would end at the same time, that could be one of the issues here, so how could we how would we account for that? It's almost like, um... Well this is kind of a difficult question to kind of answer because it's not like you know, how much time is delayed between each iteration right? Like if we knew it was a second delay from each iteration, then we could delay the first value by n seconds the second one by n minus one third one by n minus two all the way to the end, so they would start at the same time, but we don't have information, you know?
Eponymous Squirrel: Yeah, but the fact that so if you see a print right, it's printing one at a time and we know that we want all them to be running at the same time right? So put another way, so it's if you're measuring how long it's taking, like that took three seconds that took one second and then that took two seconds, so you can tell that the threads are not running in parallel, does that make sense? So if knowing that, like if the issue is how to make them run in parallel, what would you try?
Dystopian Sphinx: I'm sure there's like a... Let me see... maybe some sort of like wait method. Let's see, so we wanted to run in parallel. So kind of the question is we're going to call them iteratively but want to find a way to have them run in parallel, right?
Eponymous Squirrel: Yes, so the idea is, so for example here we'll make a thread one at a time but we expect them to start around the same time. So basically there should be little overhead in actually creating the thread such that time we started, we should be able to start all them around the same time, even if each one takes a different amount of time to run.
Dystopian Sphinx: Right and you said there should be little overhead or they should be a little, I didn't understand that.
Eponymous Squirrel: There should be little, as in like it shouldn't affect... the amount that we're sleeping should be much larger than what it the amount of time it takes to create a thread.
Dystopian Sphinx: Right. I'm looking up synchronization methods that there are in Python.
Eponymous Squirrel: That's a good thing to search for.
Dystopian Sphinx: Right. We don't have to look at locks because they aren't sharing resources. Synchronization between threads, events. event.wait, something like that? Yeah I was thinking of kind of a waiting. Wait until what? Okay, so I know I have to use the wait method and now I have to figure out how to use it. Or I have a kind of sense that I have to use wait method, kind of like thread.wait, but the question is how do I tie this... You kind of want like the first thing in the list to wait for the next thing the next thing to wait for the next thing after that until you reach the end at which point there should be some sort of like signal.
Eponymous Squirrel: Sorry, I'm not sure I entirely understand what you're saying or what you're suggesting.
Dystopian Sphinx: Right, so I'm suggesting we use... because I quickly looked up like synchronization between threads and there's a thing where like the thread might wait for an event to happen before it does anything, so I'm thinking, you know, the first thread before it executes independence, it should wait until the second thread is created and before the second thread executes or creates anything it should also wait until the third is created right?
Eponymous Squirrel: The opposite way, we don't want them waiting, we want them all to be running at the same time.
Dystopian Sphinx: I guess the reason I said wait is because like you're going to create the first thread, there's going to be some time between that and when you create the second thread, so my question is like, should we be waiting?
Eponymous Squirrel: Yeah, I think the answer is no in this case, so like right now for another way... So. If we... so do you know, what thread dot join does?
Dystopian Sphinx: No, I'm looking it up yeah.
Eponymous Squirrel: So this will block until the thread is done. And so that's the reason why everything's running sequentially now because we call make a thread number, we start the thread, and then we wait for it to finish.
Dystopian Sphinx: Okay.
Eponymous Squirrel: So the problem is that we don't want to wait for it to finish. What we want is... Or sorry, we don't want to wait for it to finish until all the threads are created, so that they're all sleeping at the same time, does that make sense? So knowing that, how would you change the code so that they're all running at the same time?
Dystopian Sphinx: Yes, so currently like the the second one is not even sleeping until the first one executes all the way, right? Well, that's interesting. It returned empty right away and then it printed the appending.
Eponymous Squirrel: Yes, so right now we're not even waiting for any thread to finish.
Dystopian Sphinx: Ah, so. So when when you remove the join, it kind of like let's a thread run. But it just returns a result from the last function? I think right, because you see like the first empty brackets? So like, like when you run it normally, sleep will wait to return until you print from threaded function until you execute because of that join, so then here the the return value of the empty bracket comes back right away and then the threads continue running I think is what I mean.
Eponymous Squirrel: But if you notice also these are in order, right?
Dystopian Sphinx: Yeah. That's almost like not exiting the make thread function until these threads, which are now in order, execute.
Eponymous Squirrel: Yeah, so how can we wait here for all threads to finish? So what are some options that you can think of?
Dystopian Sphinx: Like a condition, there's a maybe a way I could think of it. Like setting a conditional? Actually, yeah because the threads are gonna be running. Like I have a condition here, right? Because the issue that I noticed in the terminal was like it wasn't that the threads were no longer in order or anything like that, it's just that the threads were executing fast enough. You know, once the threads start running like the function was returning before the list was appended but after the function was returning that empty list, I feel like the threads were still appending to it, we were just never realizing it because there was no output to the terminal, so something like this condition like if the result equals... Kind of tricky. Hmm. Let's see. What is going on here? I'm curious as to why that didn't work because... like I thought...
Eponymous Squirrel: So yeah, this is a great check, but you're only checking it once, right? So like here maybe retry, no yeah while. You don't want while. What were you gonna do? Is there an until? So while it's not the same, right, we want to wait.
Dystopian Sphinx: Yeah, I think that might give us an error. Like maybe we just need to have something there like....
Eponymous Squirrel: That'll do it. So wait, so what happens if we run it, does it fail?
Dystopian Sphinx: I think it just wants something in there.
Eponymous Squirrel: I see. So what can we do... I know, just one? Oh what's in here, oh, oh we want numbers here, right? Yeah. That works. That's a good check.
Dystopian Sphinx: I don't know on line 22 you could just have like a one there to kind of like continue type of thing.
Eponymous Squirrel: Yeah, I mean, I think it just wants something to evaluate. And like one is a famous in Python, like it just returns 1. But yeah, what takeaways do you get from practicing this?
Dystopian Sphinx: Yeah, I think it's kind of a feeling I had to feel to kind of you know, because I'd rather have this feeling in a practice than an actual one but these are situations where I need to get more comfortable for sure because like I said at the beginning if it's something I'm a little comfortable with, it's really easy to jump off and then do it but like maybe if it's just getting familiar with standard libraries that I'm not completely familiar with or if it's some sort of tree structure that I'm not familiar with like, practicing that beforehand and getting some of those discomforts out.
Eponymous Squirrel: Yeah so I want to emphasize that. You definitely want to be studying, you still want to be practicing, but you still need to have a plan if you're if presented with something that you're not familiar with. You can't just like throw your hands up in the air and be like, nope, sorry you asked me about something I don't know, I'm stuck. And so no matter what programming problem you're faced with, you can always like break it down, even if there's one particular part that you don't know how to solve, You can break down the problem until that's the only thing you're missing and then you can try to solve that. Because I think if you have gone all this way to the point where you just didn't know what happened in make thread. At least at that point, you can just focus on what would a make thread function looks like, instead of having to envision the entire program.
Dystopian Sphinx: Right. I see. So like, would you suggest kind of in the beginning just focusing on purely just the logic or at least... I don't think I did a good job of that. I think like I could have just very easily talked through the logic a little bit better with my interview.
Eponymous Squirrel: Yeah. I would think about it as writing pseudocode, right? So in interviews, this has served me well, which is like I'll read through the problem and... I feel like there are two ways of approaching it: either you look at the problem and you think of your solution or just start typing code and like this is what it would look like even if I don't know all the pieces inside of it and then I can just focus on one of these at a time. Right? I feel like just writing the scaffolding of the program can help you think through how to solve it. And it's a good way of dealing with with situations where you're not entirely sure what the answer is from the beginning. And not only that, if you don't write anything then it's hard for me as an interviewer to know exactly where to guide you whereas if you got to the make thread problem function is the thing that you're having trouble with, then it's a lot easier for me to give you hints in that case.
Dystopian Sphinx: Right. Cool. And like, just in general would you... like I got feedback from another interview to really know the standard library, is that something you focus on or would you just go back to kind of the plan idea of being able to have an idea or whatever.
Eponymous Squirrel: What was that?
Dystopian Sphinx: Like I got feedback from previous interviews to like really know the standard library like in and out would you recommend that advice or do you think like having that plan is more important than really knowing anything completely in and out?
Eponymous Squirrel: I would say that it's... You definitely get brownie points if you show that you're comfortable in your language of choice. And learning about the standard library is part of that. But I can tell you that for this particular problem, if someone doesn't know threading I don't deduct points. Because for me I'm getting signal if I see... because I'm also understanding how you look up documentation and how you can understand that and use it right, so for me it's very little... it tends to be less about knowing everything and more about just what your process is to get unstuck.
Dystopian Sphinx: Okay.
Eponymous Squirrel: But again, learning more about the standard library will still help you. I just think that it's a little bit unfeasible to like really learn everything there is in the standard library or at least at some point you start having diminishing returns.
Dystopian Sphinx: Make sense. And then like for this make thread, this thread target, like your extension class, are these the common parameters, you have some sort of function and arguments to a function that kind of...
Eponymous Squirrel: Are you asking about grading specifically or?
Dystopian Sphinx: Yeah like line ten, like the the reason like just why the class would take a threaded function. I guess I could study that on my own, but I'm kind of interesting that like I'm wondering are there like, instead of sleep, other parameters or other functions that you would have in that third function. I'm just curious.
Eponymous Squirrel: Thread function can be anything, like it can take as many parameters as you want.
Dystopian Sphinx: Is like the purpose of that third function to kind of like define some behavior for the thread, like it's going to sleep?
Eponymous Squirrel: Yeah so the purpose of it is just going to run in a separate thread thread, in a separate process, but this is how you define what code it's going to run. So whatever threaded function does is what it actually runs after it starts.
Dystopian Sphinx: Oh okay cool, I see that. Any tricks you have when you feel like like... are you the type person to start writing things down or do you ask questions? I want to have something like first step if I kind of feel really stuck, like do you have any suggestions to break that inertia?
Eponymous Squirrel: I mean, you're definitely free to ask questions. I would just suggest that the questions be specific. So telling me that you're stuck, right that's like okay, it doesn't really tell me much, but if you're like telling me that you need to figure out how to make a thread, then I can immediately help you with that, right? So if you're feeling stuck, just sort of identify what it is that you're stuck on. And that's the information that you want to share and you want to get help on.
Dystopian Sphinx: Right. Yeah, and I think one thing I'm gonna stop was I didn't want my interview to think that I kind of didn't know how to use a thread and I was rushing to kind of figure that out and I made that mistake before and I guess I'm just learning it again that it's just better to say, okay, this is something I'm trying to figure out.
Eponymous Squirrel: Yeah it's rare to share too much information with your interviewer, like you can totally say oh, I'm not familiar with how to make a thread, I'm gonna look it up. Like that's perfectly fine.
Dystopian Sphinx: Okay. Guess there's not much time left, is there anything that I could do to make this more... I think now that I'm more comfortable with the threading function, it makes sense like the direction that this is going a lot more.
Eponymous Squirrel: Yeah, so I was thinking through and see... oh wow, that looks horrible. I guess new lines don't copy over. Well, but I guess you can read like to the dashes but yeah, basically I was... remember that when someone's interviewing you, it's not just about you solve the problem, it's about how you do it. So a lot of my feedback was on like if you're taking a remote interview, then be sure that you're walking your interviewers through what you're thinking and then just practice breaking down problems I would say.
Dystopian Sphinx: Right? Cool. Yeah. I'm not upset about this interview because I'd rather have this kind of experience than like an easy interview, so that this was really good for me.
Eponymous Squirrel: Yeah, and I totally commend you for practicing this way. I think it's a great way to prepare.
Dystopian Sphinx: Cool. Any other feedback?
Eponymous Squirrel: No. Again, just keep practicing. I think you're on the right path. Totally my pleasure. Good luck on your further practice interviews.
Dystopian Sphinx: Alright yeah, thanks again for the patience and uh, you know, I think I'll know what to do next time or I have a little bit more of experience under my belt.
Eponymous Squirrel: Sounds great. Hope to hear of your success. All right, take care, 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.