We helped write the sequel to "Cracking the Coding Interview". Read 9 chapters for free

Python Interview with a FAANG engineer

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

Interview Summary

Problem type

Insert Interval

Interview question

You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval. Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary). Return intervals after the insertion. Note that you don't need to modify intervals in-place. You can make a new array and return it.

Interview Feedback

Feedback about Magnetic Buffalo (the interviewee)

Advance this person to the next round?
Thumbs upYes
How were their technical skills?
4/4
How was their problem solving ability?
4/4
What about their communication ability?
4/4
- Excellent communication, easy to follow and understand - Good work with clarification questions. Your immediate intuition to use binary search is spot on and shows experience with algorithmic problems - Normally I absolutely encourage the use of pseudocode but given the time constraints meta imposes, be mindful of spending too much on it so it does not cost you down the line. - I absolutely love you sense of humor, highly encourage you to bring this spirit in the interview, it makes you a fun candidate to interview and also showcases a semblance of confidence which biases the interview into trusting your expertise. Nifty nuances of the interview process right there. - Excellent work to do your complexity analysis right at the start. This saves you time and checks a big box in the interview process which is invaluable. - Kept a good pace but we should have ideally have had a solution at 7:22. This is a problem commonly encountered with Java usage given its constraints on data structures. Feel free to experiment interviewing with different languages you are comfortable with. - Good understanding of traversals overall, you weere spot on with in order here. - Good technical understanding of the CBV and CBA nuances which end up infliuencing the complexity. I tend to prefer the use of encapsulation or using a class level map structure which is accessible to all methods of the class. You can then insert values to the map/ list

Feedback about The Legendary Avenger (the interviewer)

Would you want to work with this person?
Thumbs upYes
How excited would you be to work with them?
4/4
How good were the questions?
4/4
How helpful was your interviewer in guiding you to the solution(s)?
4/4
Interesting set of questions. One thing I didn't mention is that I've received contradictory advice about Meta interviews. Yours was that the code can be "good enough" to enable me to move to the next problem. Other feedback I've received has been that the code needs to be basically flawless on the first try. It might be worth discussing this with candidates to calibrate their expectations (perfection is a terrifying standard).

Interview Transcript

The Legendary Avenger: Awesome. All right, so maybe just before we get started, maybe give me a quick run through of what you're prepping for as well as what you're looking to get out of this that we can calibrate it for.
Magnetic Buffalo: Sure. So I'm preparing for a full loop with Meta next week. I'm basically just practicing until things become boring.
The Legendary Avenger: I like that philosophy.
Magnetic Buffalo: Yeah, I tend to get nervous. I've been pretty hit and miss on coding mock interviews. It's my weakest area out of the three, so we'll see what happens.
The Legendary Avenger: All right, I have some good news for you. So I actually work at Meta and they do conduct interviews there. So if you'll like, I can give you a META formatted interview right now. All right, so just before we even start there, I'm hoping you know the general format of how Meta does things. Like, have you been given a run through by your recruiter?
Magnetic Buffalo: Yes. So I have the notes here. One was that you'll see an easy medium or medium medium or maybe medium hard. So two problems in each interview and that you're looking for problem solving, code quality, edge cases, bug finding, and communication.
The Legendary Avenger: All right, that's exactly it. And so I'll even add a further bit of tip there. Don't practice outside of Leetcode. I tell my candidates for Meta, 9 out of 10 questions they'll ask you will come from the Leetcode list. So if you go into Leetcode problems, company problems, in this case Facebook, and then sort by frequencies in the past six months, most of your questions are going to come out there. I can give you a guarantee of that. And they're right. Yeah. So Meta typically doesn't ask hard questions. They usually ask an easy to start with. If you get that done, they'll move towards an easy medium. If you get that done, it might be a medium medium or a medium hard. But the expectation is you have 45 minutes and you have to get through at least two questions. That's a general goal. So two questions within 45 minutes, that's about 20 minutes per question. This makes sense.
Magnetic Buffalo: Yes.
The Legendary Avenger: If that sounds like something we can do, I think feel free to select your language and they can get you going with this. Process. Let's do Java. Good old. It's been a while since I've seen Java. One second right here. So this is the first question and take your time, read through it and then whenever you're ready, feel free to propose a solution and code whenever you're ready.
Magnetic Buffalo: Okay, so let me see if I understand. Given an array of non overlapping intervals. Okay, so I imagine that's like an int array. Array represents start and end of the ith interval and intervals are sorted in ascending order according to start. You're also given a interval new interval start and end that represents the start and end of another interval. Certain new interval intervals. Such intervals are still assorted in. So in ascending order. And intervals does not have any overlapping intervals. Merge overlapping. That's. That's the key.
The Legendary Avenger: Exactly.
Magnetic Buffalo: Inserting necessary turn intervals after insertion. You don't need to modify internals intervals and places. You can make a new array and return it. That's. That's convenient. I haven't really thought through how I want to do this yet, so I'm going to start throwing down some thoughts. One is going to be that I can probably do a binary search on the start index because it's organized by start index. One possibility would be to extract spot indices. Search that. Except I want. Are there any cases where multiple interview intervals like line up and I have to insert there. So if it's like 2, 4, 2, 1, you know, 5 and then I have to insert like 1, 2.
The Legendary Avenger: So let's say, let's put a distinguishing aspect. I'll make a note of something distinguishing here the input array. So the one you start with, it has non overlapping intervals, but the ones that you're supposed to insert could potentially overlap with some of the intervals already defined. So the one you start with the input array, it could be an empty list or it could be a list of intervals that do not overlap.
Magnetic Buffalo: Yeah, okay, that's not so bad. So that would become 1, 4, 5, 6. Okay, so the binary search suggests itself simply because it's already sorted in some fashion. That's always a sort of a popular trick. And also I have the advantage that I know that the start position is unique because we've just established that there isn't an overlap. So that makes binary search behave nicer. I don't have to try to find the front edge or the trailing edge. So really what I want to find is I do a binary search. I can get the index or the index where it would appear if it were. If it were present, and then I can copy into the array. Okay. Before that, determine if mesh to left to right copy into the array. Okay, so let's have a look at this. This part O log of N peps O N. Just doing a little inspection. And this is O of N as well. So it looks as though O of N is the best I can get out of this unless I think of something clever. Okay. How does that analysis seem to you?
The Legendary Avenger: I was going to say look at this bit right here. So I like the idea of binary search. I think it's important. But for the modification, make sure you keep a note of this. So in case we have an overlap on insert, we want to do that modification in place.
Magnetic Buffalo: Right.
The Legendary Avenger: So that bit about O that might be disqualified if we leverage this because we want in place modification.
Magnetic Buffalo: Right. I'm a little confused. When you say in place modification, do you mean on the input or on the output?
The Legendary Avenger: We receive an array. So let me just point that out. So we receive an array here, and this array of ours is our input. And it has. You can think of them as tuples, but they should be mutable. And so these are tuples containing the start and end. So it could be start, end, and you have repeats of this. And so then you receive another input, or you could receive multiple inputs. But for now, let's think of this as just being, you know, input two here, another range. This is just a start end. Now this start here might overlap with some of the entries here, in which case we want that margin to happen in place. So you modify the tuple within this in place. So if, let's say this start is the new end, you should be modifying just that bit right there, the end, in case it's a new entry on its own, let's say it doesn't overlap with any other entry, then say enough, in which case you do an insertion at position and then push the rest. So yes, it's still oven, but the key bit that I want to note is we want to make sure, in case we have let's say a start like this and an end like this. You're doing that in place modification of the tuple in the input Makes sense.
Magnetic Buffalo: Okay, I think I understand what you're saying. The reason I was asking is I was a little confused about whether we were modifying the input array. So I'll drop copy to new array is the thing that happens. It may be that we work on this in a list for convenience to make it easier to chop things in and out.
The Legendary Avenger: Yeah, and I think. Yeah, and to your point, that's actually a language nuance. I feel like with Java there's a clear distinguishment between lists and arrays, and so sometimes properties get miscommunicated because with the likes of Python Rust and such, you just make them mutable or just at least by default, so they're automatically extensible.
Magnetic Buffalo: Yeah, yeah. And like, my personal favorite is Ruby, but almost nobody knows it, so I use Java.
The Legendary Avenger: I quite like Ruby, actually.
Magnetic Buffalo: Yeah, it's a lovely little language. I don't feel that it scales to very large code bases very well. You, you have to sort of use tools like Sorbet to get static typing to make it tractable, but as a, as an actual experience, it's lovely. Okay, so public int. Array array array array intervals. All right. Okay, so I'm going to try and start filling out my logic here. I might jump around as things occur to me. I like to write things down as soon as I think of them. One thought that comes to mind is that for binary search, I might not have that option. Do you mind if I look up something on Google real quick?
The Legendary Avenger: By all means, yeah. It's a generic algorithm.
Magnetic Buffalo: It is, but I don't know whether it's defined for lists.
The Legendary Avenger: I would say, okay, just do this. Run me through how your binary search will work and then assume that you have a function that will do that bin search for you. That will happen for you.
Magnetic Buffalo: Happily, the collections class does have binary search defined, so I can just take a look at that.
The Legendary Avenger: All right, that's perfect.
Magnetic Buffalo: Usually I'm using arrays binary search. Right. Okay, so convert to list for int pair in intervals, make it an array list. Because we want fast lookup of positions. Yeah, we don't want to do on lookup. Each time we go to a position and insert something or remove something needs to be given a name. Interval in intervals, we say interval processing at interval. Okay, so now we've got the list. Why is this nat here? List cannot be resolved to a type. Well, trust me, it exists. Yeah, the intellisense is having some trouble. Okay, so what I want to do is binary search. So what does that give me? Searches specify lists, a specified object using binary search prod algorithm and you can give it a comparator. So it would be something like collections binary search. And we would take the interval processing list and instead of a key I would give it a comparator which takes left and right and return integer, compare left zero to right zero. So this is comparing the start positions. That's what we're looking for. And I need to oops I do need a key of what I want. Let me think. How do I want to do this? I'm interested in what I should be doing here. Type key. I guess it's an int array. Oh yeah. Okay. Actually, you know what it is is groontable. So what I'm looking for is the position where either something like that exists in the case that it's a repetition of an existing interval. Which would be interesting. Okay. And that we change int. No, not array, index, just index. If index is negative. Actually. Sorry. If it's greater than or equal to zero. Okay. So if index is not greater than or equal to zero, if it's negative, then what we're being told is the position at which it would be inserted. Right. So that means we would insert it there. So how do I want to do this? I guess I want to go and grab the FDOF equals. I feel like interval processing is going to have to change as a name. But I haven't got a better name yet. Just thought, okay, and it'll write off simple processing. Get index plus one. Okay. Oh, wait, we're saying negative. Got to negate them. Okay. So if FDA of zero is less than zero. Actually, you know what I could do is I can just use max and then rather than zinc green. So we go left of left of zero and interval zero. And then this is a little processing cat index. Now I haven't accounted for the case where there isn't any merging. At the moment I'm just assuming overlap. So I need to do some checking for that. What else do I need to do? I mean that might be something you hoist into another method somewhere. Whoops, there's a little type error here. Okay. Yeah. Okay. So. Well, there's three sorts of cases here actually. There's overlaps left, overlaps right, overlaps both and other.
The Legendary Avenger: Exactly.
Magnetic Buffalo: This reminds me of another problem, but I don't remember the details. I feel like I've dealt with a similar sort of interval processing problem before.
The Legendary Avenger: Yeah, there is an equivalent that is more popular called I believe it's merge intervals. So rather than you just get an array and it will merge the intervals that is like the subset of this. Because in that regard it's basically the same problem, only that it's reworked. In this case, you start with non overlapping array, but by and large they're the same. You simply have to look at that and make sure it falls within the range of the next.
Magnetic Buffalo: Yeah, yeah, that seems. As I said, it seems familiar. So overlaps left is if the End of. I need to move left of and right of up so I can refer to them. Left of and is greater than, greater than or equal to new integral zero is less than or equal to one. Okay. If I left, this is where I would actually do the logic to establish merged left and merge right. So I'm going to define those here so I can refer to them later. And I'll give them a nonsense value which will pop up. If I've got a bug, I'll be able to see that I messed it up. Okay, so if it overlaps left. Oh, wait, left off. I've got a repetition here. Sorry. How many would you do of these in a given week?
The Legendary Avenger: In my case, because I interview people, I actually end up getting passive practice for context. I know how to solve this question, but I haven't coded it myself. I have a solution, but by and large, I get my practice by interviewing people. And oftentimes I repeat the same problem about 10 times because, you know, I interview. And so by the time the week is done, there are like three or four problems that I really have a mastery for. So that's kind of. It's a bit hacky. But I get practice by conducting interviews.
Magnetic Buffalo: That's cool. That's a neat way to do it.
The Legendary Avenger: Yeah. For me, I find that, you know, observing oftentimes is the best way to learn. Like, you get to see what strengths other people have as well as areas that you want to avoid, which is usually it's. It's underestimated. What do you call it? It's understated. It's an understated way of learning.
Magnetic Buffalo: Yeah. Yeah. Well, I guess you get to see lots of different solutions and you also get to see, like, what the dead ends are exactly. You know, where people get stuck. Equals new interval. Start. Oops, went to whiteboard accidentally. Where was I? Interval. Start. Merge right equals new. No, no, wait. Overlaps right equals write of zero. Sorry. One and an otherwise merge left equals left of zero. Something about this nested if makes me wonder if there's a more sort of generic solution that works correctly.
The Legendary Avenger: I'll say. I personally like the approach you've taken. The only thing I might have pointed out is perhaps if we had, let's say, done a fan out. So what you did was the first step, the binary search step. I think that's very sensible. So find the insertion position. Then once you find the insertion position to your point, it will either be within another array. In fact, what I'll do, I'll probably find the first Array. So my binary search will be custom set that I'm finding the first tuple where it's either less than or equal to my start insertion value as well as the end the next position in that case being less than. Although I don't even need to care about the next position. I only need to start care about my start position. So if it's a less than or equal, that's what I need to be looking at. And then I basically do a far note left. So if, let's say I merge with the left, which is exactly what you've done, do that you pick the small of the two margin, the right pick the grid of the next one and my end. So just doing that automatically guarantees that you've done an insertion and you either do an insertion or you don't. And so step one, find the find the position. Step two, fan out to the left, funnel to the right. Which it's pretty much what you've covered. Only that in this case you have a lot of ifs rather than clearly separated two step. Right.
Magnetic Buffalo: Yeah, yeah. As I said, the ifs make me nervous. Something that's complicated. Yeah, I don't think this is a complete solution at the moment. The thing I'm not dealing with is getting rid of the existing values in the case where I completely replace. Ok, so the else case is like do nothing. Okay. Overlaps both means that everything goes away and so left of gets deleted. So interval processing. Delete, remove index minus 1. You usually have to do an offset to account for zero. Okay, what was it? Plus one. It's also getting repetitive as well. No, I only want to remove one side. Okay, so if it. Yeah, if I want to do. Sorry. Yeah, one thing at a time. Check to the right. I want to get rid of the one position to the right and the same for left. We're pretty far into coding this and I'm starting to get sort of like answer my pants about walking through it. If you feel suitably, I'll say I'll.
The Legendary Avenger: Trust you on this. I think you communicated the general idea well. And so given that you're on minute 25, I want to make sure we have sufficient time for the next question. So let's do a transition. I think you've done the complexity analysis, which is awesome. So I'll trust your solution on this. Plus you know, you already told me bin such I know you have the good solution, the optimal solution in this case. So let's transition to the second one and just give me a second as I find this. It's one of my favorite questions. I quite like. Quite like how it works. So send it your way. So I'll paste it right above so that we have the orders well defined. Oh, let me actually do this. So this is Q1. And so this is our second question right here.
Magnetic Buffalo: Right.
The Legendary Avenger: So you're given a binary tree. In this case, the input is just the root. And I want you to imagine yourself being to the right of the tree. And I would like you to return the values as you see them from the right top to bottom.
Magnetic Buffalo: I remember this one from the list. Is it okay, or would you like a different one?
The Legendary Avenger: Okay, you've seen this. Then let me find another one. Okay, so you've. You've done good practice, which is good. So let me find.
Magnetic Buffalo: Yeah, I've been. I've been grinding the top, like 30 problems.
The Legendary Avenger: Perfect. All right, so let me find a second one here. Another one that I quite like. So I've basically. I've tested you on an array problem. So typically I try to mix an array and a tree problem.
Magnetic Buffalo: Okay.
The Legendary Avenger: So one second. So I'm gonna stick to this one. If you've seen it well and good, we're gonna just stick with it anyway.
Magnetic Buffalo: Okay, cool.
The Legendary Avenger: And I might ask you various traversals. So there we go.
Magnetic Buffalo: Yeah, yeah. Two knives. This. This is number one on the list.
The Legendary Avenger: Great.
Magnetic Buffalo: Yeah, it is. It is the top. The top. Most asked question is the vertical traversal.
The Legendary Avenger: Okay, perfect. In each case, do this. I'll throw in a challenge here because I think trees are important. So do the vertical order traversal. In addition, I would like you to do the following in order, three, order. And I'm only asking you to do this because I know this is practice. And I'm going to force you to go through all the traversal techniques in case you're caught by any of them. Right.
Magnetic Buffalo: Oh, interesting. Because usually I solve this with BFS because the BFS keeps track of the depth for me. Let me think about this for a second. I remember. How did I do this? So when I first saw this problem, it looked to me because it's a binary tree. It's not a binary search tree, though not specifying that let me think it was in order traversal that played back column by column. And it was relatively effective at that if I kept track of, like, plus and minus. But it wasn't good at vertical order on the right side of the tree. Right, so what do I mean by that? Yeah. Right. So this tree, if you go in order, you go two, three, One you descend or other you arrive at one, you spit out one, then three, then two. So so far your columns are separate. Where it actually let me do this more completely, where it starts to go wrong is that like you expect 5 and 7 to be in the same column, but when you do an in order traversal, what you get is seven then five. You're not getting a top down, you're getting bottom up. So it works fine for the left tree, but it breaks down for the right tree if you've got this kind of structure. The other thing to bear in mind is that in the problem definition they say that 6 would be in the same column as 2. Whereas with in order traversal, 2 gets a column by itself, it gets treated as a single column.
The Legendary Avenger: Let's clarify that a bit more so in this regard when we have the six right there. So, okay, what are you saying about in order? You're saying seven will come before six?
Magnetic Buffalo: Sort of. So there's a couple of different things going on that I talked about. First of all, there's like the way that 7 and 5 are dealt with. So 7 and 5, what we want is 5 7. What we get is actually 7 5. So it breaks the rule that we have to be top to bottom, column by column, because we go to two, we turn right, we turn left, we tend, we do six, we do seven, we do back to five. So you wind up with seven five. And I'm sorry, probably what I didn't mention is that I'm maintaining a map of column number to things that have been encountered in that column. And you subtract one if you go left and you add one if you go right. That's pretty effective at keeping track of where you are horizontally. Most of the complexity in this problem comes from doing it vertically. I never did the DFS solution, but my understanding would be that you keep track of depth manually. So just to sketch some pseudocode, you'd have first, go ahead, I was going.
The Legendary Avenger: To say, because I trust that you know the vertical order traversal. Let's actually, because you're right, I think the general idea of using depth point in this case, so you start root zero, left, right, and you're keeping track of the level you're at for the depth. I think that's the direction you've mentioned, which to me makes sense. That's actually how you implement this. So let's focus on these three because you know, the input is the same. I want us to make sure we implement the three main types of traversals. I Feel like vertical order, level order. Those are usually unique types. So let's go with these generic types because I feel like they never get as much love nowadays. So in order. Let's start with that. So what's the order for in order? Traversal, particularly.
Magnetic Buffalo: Okay, so let me see. I get 2, 3, 1. 32 goes to 5, down to 6, 6, 7, 5. That was for in order, right?
The Legendary Avenger: Yes. That's good. It's left. So we're taking our left and then we're taking our root and then we're taking our right. Right?
Magnetic Buffalo: Yeah. So we got the left tree which comes back in order. We get the root which is in the ideal for the correct position. We get a slight reversal, which is that 6 comes before 5 because it's on the left branch of 5. And then we go past down to 7, and then we return to 5 and conclude.
The Legendary Avenger: Perfect. Okay, that is perfect. Okay, so let's get an implementation for this and then we'll discuss how we can leverage the same for the pre order and post order.
Magnetic Buffalo: Sure, sure. So, public.
The Legendary Avenger: I quite like this because for context, I like this problem because you knock out about seven or eight problems just by doing this, which is usually awesome because there's so many problems related to the same type of traversals.
Magnetic Buffalo: Traversal. I'm going to assume that you gave me some sort of node and then you gave me the root. And I'm probably going to do DFS with get root zero. Look at that. Sorry, not introduction. What do I want it to do? I'm going to get it to modify a map in place. So make this void just for the moment. But you could also pass a map. Actually, I'll just do pass in a map. So map of integer to list of integer. I always like to say what this.
The Legendary Avenger: Is, just out of curiosity, is Java. Call by value or call by reference.
Magnetic Buffalo: Call by reference. Except for primitives.
The Legendary Avenger: I see. So in this case, if we are passing.
Magnetic Buffalo: Yeah, here's the thing. Why am I using the object type instead of the why am I not doing this? The answer is you can't use primitive types in generics.
The Legendary Avenger: I see.
Magnetic Buffalo: With a weird twist, which is that you can use primitive array types in generics. I don't understand what the logic is. I know that they're very smart. That's a good reason. It's a piece of trivia I've memorized to get my job done. Okay. Column. Map. Okay. Private. Now we got it modified in place, so do process command. And actually this is going to be a flat list of integer because we're doing a whoops. Traversal. Okay, Private void DFS with depth. Okay, so we're doing in order first. So that's basically go left, do stuff, go right. Okay, so DFS with depth. Oh, I also need the column as well. And you can argue that like depth is a bad name at this point. It could be, you know, depth and depth, sorry, row and column, for example. Right. I think from memory, the intuition for this problem is that you think of it as being on a grid. Okay?
The Legendary Avenger: Just like so.
Magnetic Buffalo: Okay, so we take the root, we go left. Apparently I can't type. We go left, we add one to the row because we're going down. Since we're going left, we subtract 1 from the column and we pass through the column map. I haven't defined what do stuff is yet. Tbd. I just want to get these out of the way. Root right row plus one column plus one column map. Okay, so the processing I want to do here is to say that at column map, I'm going to put. Oh, no, sorry, let me go back a second. So I need to get the list of integer of scene values equals column map. Getall default. And the key is the column and the default value, it's going to be a new array list. So this is in a case that hasn't been established yet. Then scene values add root val and then column map, put column. Okay, so that's the in order traversal there. Now let me think. I need to think about the use of the row. I'm carrying it around, but I'm not using it at the moment. Right. So I've got an in order traversal, but it's still going to have that paradox where seven comes before five. But how do I want to deal with that? I mean, there's a couple possibilities. I can sort of come up with a compound data type that keeps track of both the value and what column it was in. I can turn it into like a matrix and fill in positions. No, that doesn't really help. That just repeats the question. I guess I. Yeah, I guess I do want to keep track of the road that something was found in. And I'm trying to think. This is where I got. There's a couple of possibilities. So one is that I define coordinates to be in row in col and then I change the map to be coordinates.
The Legendary Avenger: I was going to maybe ask because in this regard are we ever going to insert at any other position other than that? Like given where you've called this or at least you perform this insertion, is it really ever going to vary or is it always going to be an insertion to the right?
Magnetic Buffalo: No, it's not always going to be an insertion to the right. That will depend on. I want to depend on the direction we came from, actually. So if we.
The Legendary Avenger: What if you do a generic traversal, because you have the general in order structure, right? The recursive calls make sense. So this will be called before any insertion is done. And so it will basically go all the way to the left of the tree up to the one. At that point you can do an insertion. And the next recursive call in the call stack will be the one that contains the three as the current node. And so that's the next one that will get into this stage. So you can perform an insertion at that stage followed by the right, if any. And so from that perspective, we technically should be able to go to get away with just inserting and not having to worry about column positions. You see that?
Magnetic Buffalo: Let me think about that for a second, see if I agree. So is your argument that row is all we need rather than column?
The Legendary Avenger: I'll even say we don't even need to know the row and column values. They're all encompassed in the way you made the recursive calls. So with this structure, right now, this should traverse all the way to the left, at which point, once you can no longer traverse, you do the insertion. Because the last call, the call that has the value one, that will be the first one to enter this section of the code here. So if you have to do a debug, the first call that will make it to line 42 will be the call that is at position one. The rest of them will be in the call stack. And the next one to drain will be the one that made call three, which then followed by call one and the next one to drain will be the one that made call to the right, which is the first ones, because, you know, we kind of look at them in order. So left and then because the immediate one, the one that's going to the right, is the first one that, that went through. So that will be the one that made it to one. So that's the. Sorry, the one one doesn't have a right, that will be the one that made it to three, because that. On media.
Magnetic Buffalo: I think I understand where you're coming from. So I misunderstood. I thought we were still trying to do the vertical order traversal rather than just a straight in order. That's. That's much simpler. Yeah, we don't we don't need all this bookkeeping. In that case, I'm sorry, I misunderstood what you were saying.
The Legendary Avenger: Totally fine. Yeah, maybe I should have clarified that too because I figured. Okay, I think I said I trust your vertical order traversal given you've seen this problem. And so I wanted us to just run through the three in order, pre order and post order. So if we just get those then we should be fine.
Magnetic Buffalo: Yeah, sorry about that.
The Legendary Avenger: Totally okay. Don't worry.
Magnetic Buffalo: It's one piece of feedback I've received is that sometimes I over fixate on the original problem.
The Legendary Avenger: It happens. I also should have communicated better. Frankly, I wouldn't dink you points on that one because to me I tell my candidates what I'm looking for is signal. I'm not looking for a solution. And so if I pick up the signal I'm looking for, and this is usually the ability to navigate different problems, the ability to analyze the problems, test your pro, test your solution out, etc. And catch bugs whenever any appear. And also some non technical signals. An example, the collaboration openness to feedback, then you're good. It doesn't matter whether you get a solution or not. It doesn't matter whether you pseudo code for some of your code sections like the solution is probably one of the least important aspect. It's good to get because again there'll be a lot of really good candidates with all those qualities that also get a solution. So that's the only reason I encourage my candidates. Make sure you still get a solution. But at the end of the day signal can be inferred from a lot of areas. Right.
Magnetic Buffalo: Okay. Yeah. Much simpler once we. Once we drop the requirement and just focus on traversal in order. I believe this is correct.
The Legendary Avenger: Yep, that's certainly correct. Yeah. All right. What modification would you make for pre order and post order?
Magnetic Buffalo: Pretty much this or this. There is to be one thing to bear in mind. If I haven't dealt with the null case, I should just quickly dispatch that basically do nothing. But yeah, most of what you're doing is moving the accumulator operation to either above or below or in between. There was a time I didn't understand the difference between in order and pre order and post order and I have an interview and it was a disaster.
The Legendary Avenger: Yeah. I always tell my candidates I like forcing people to go through this structure. Like in fact oftentimes when I'm doing practice, I start with this. Just have them know that the owner of calls is also the order of traversal. So in order like the Statement, you know, it speaks for itself. So you want to go through things in order. What is, what is in order? Typically we go left, then root and right because it's in the structure. Right. So the smallest is usually on the left. And similarly for pre order, that's kind of implying a backwardness. So, okay, so take the root and put it at the start and then everything else stays the same, similar post order. So just having that understanding and seeing that recursive calls reflect that, it saves a lot of time because then it's a matter of justifying why that would work in case the interview is interested. But nine out of 10 interviewers, once they see that you have an understanding of this recursive call structure, they let it go because why will they bother you? Now that said, there's an extra problem that I think throws a lot of candidates off because this is using recursion. But you can technically solve this using an iterative approach, which is much more challenging. And doing it, yeah, it's.
Magnetic Buffalo: You have to maintain a stack yourself. It's not something I practiced exactly.
The Legendary Avenger: And, but that, that, that's the practice that I tell people. Once you have a general understanding on how to get a solution to start with, then advancing to that is probably beneficial. It's a good optimization overall. Right. All right.
Magnetic Buffalo: But anyway, I mean, I'm, I'm skeptical about it because I, to me, the only advantage of doing iteratively is that you move memory from the stack to the heap, which tends to be a lot larger. So it makes sense if you have a very large tree to do it that way. But in terms of the amount of. I don't know if the performance is actually that much better, I suspect for a smaller tree, it's going to fit in the cache anyway. And so is the call stack versus having a stack where it's sitting somewhere out in memory and it's probably a linked list with a bunch of pointers to follow. So I don't know. It's one thing I'd want to, I'd want to test it, you know, with, with realistic production level sort of data to see which one. How they behave.
The Legendary Avenger: Fair enough. And maybe a quick, A quick. This is probably the one. You know, it's not, it's probably related, but not as big on a technical point of view. But one candidate I saw when we were trying to optimize it further, because there's a way you can do this with constant space, I believe seen a solution broken down in litcom before.
Magnetic Buffalo: There's Morris Traversal which I don't understand, but I know of.
The Legendary Avenger: But here's a funny one. So a candidate I was working with, they were building this in Python. I don't know the same generator structures, actually. I know Java also has some of them, but rather than leverage a static list, they leverage the generator so that they could, you know, and all generators also get murky underneath. But the whole idea was in the accumulator. Rather than adding everything to a static list and dedicating memory to it, they added it to a generator structure and then drained it at the end. And so this was based off of a use case and it was impressive in itself because while yes, technically doesn't change the complexity, it was a good signal of that candidate's optimization skill. And this is why I tell my candidates, you see how optimization can come in different ways. So this candidate ended up showing their knowledge of generators and how they could use it to save memory in a production situation. And that's it. That's the kind of thing that I tell people stands out. Like you could get an optimal solution that's running in constant space. Sure. But I am willing to bet you that very few candidates would think to even suggest the use of a generator in that kind of situation. And those are the candidates that stick out. It shows you they have production experience. Right?
Magnetic Buffalo: Yeah, yeah. I mean, so you can imagine a couple of different things where you would change this to be an in stream.
The Legendary Avenger: Exactly.
Magnetic Buffalo: And this will become new in stream. And I'm trying to remember what the thing is for. I think it's just add. Yeah. And there's. I mean, part of the advantage, one of the reasons you would do this is there's definitely a way to add stuff to it. Where is it? Why am I not seeing it? Anyway, I'll put that aside for the moment, but assuming that you've got some method for adding to the end stream, then you can return the stream and then the user has the choice of whether they immediately perform operations on it. Do they accumulate a dot min might define the minimum value out of the set. Do they want the average? Do they want to turn into a. Turn it into something else as double stream? Right. They want it as doubles instead of integers. So yeah, there are advantages to turning that into an int stream. It's a little. It's not that common to see strings passed around in production code as the sort of the return from methods. You do see it in the JDK a fair bit. So yeah, I would mention it, but I don't think it's as much of a slam dunk as generators are in Python.
The Legendary Avenger: I agree. And that's the thing that point on mentioning it, that's usually what you're looking for. And that's the thing, let me give you like stages of feedback, not feedback of signal pooling there. So most candidates, I expect them at least the candidates that pass, I expect them to get a solution at the very least show me how they would do the in order traversal. But for my senior and staff engineers, even if they don't get this, I expect to hear some technical considerations that, you know, let me see if I can find a good example here. Yeah, you know, we usually jump to the generic sorting examples, bubble sort for instance, it works quite well with smaller lists. It probably outperforms the likes of merge sort. And the reason is as you do the splitting and all this, actually there is a breakdown. Let me see if I can find that for you. But bubble sort has been shown to out in an objective. It's all of N squared, but it actually outperforms an N log N algorithm when it comes to bigger lists. And if you do the numbers, you'll probably see that implied even in the, even in the complexity analysis. Let me see if I can find the article.
Magnetic Buffalo: This reminds me of feedback I got on a system design which is trade offs. Trade offs. Trade offs, exactly. Needs to talk about trade offs.
The Legendary Avenger: Precisely. That's exactly it. And so in this case even the conversation on stream usage, it's probably not necessary, but being able to even hold that conversation, that's it, that's the signal. You want to show that you're aware of some of these advanced techniques, then they're not paramount. But if you find an interviewer who actually engages you in that conversation, that's always good signal. It showcases that they clearly see, they clearly are trusting your intellect to a point where they are engaging in more advanced structures, which to me, I always nudge my interviews in that direction because I feel like you get caught with. You get caught off guard when you're focusing on just the fundamentals because oftentimes that's not the only thing that you're touching on in code. And so when you get into more advanced topics, it kind of allows you to escape some of the basic fundamentals which frankly most of us will flunk. You know, give me five interviews and maybe I'll get three of them correctly and fail in two of them because of some of those nuances. So you want to escape those and go into the comfort zone of more advanced topics. Which are more open to conversation rather than being grilled or such. But you know, that aside, I wanted to make sure that we run.
Magnetic Buffalo: That's interesting. And actually I can imagine something like changing this interface so that you pass in a consumer that you want to use. Consumer of t accumulator. And the reason being that you then have accumulator apply. Sorry, accept takes thing and the user provides the logic that reacts. And their logic might have a list. It might be doing a summation, it might be doing reservoir sampling. You don't know or care. Your whole job is just traversal.
The Legendary Avenger: Exactly.
Magnetic Buffalo: Interesting. Yeah, that's good feedback. That's useful.
The Legendary Avenger: Sure. Yeah. And I wanted to just run through the note of staking that way we at least make sure we also run through that. So I want to point out a couple of things. Your communication is excellent. I feel like you've probably been told that a lot and I'll iterate on that. Like you have really, really good communication. You're easygoing, easy to co work with. I felt happy during this interview, which is awesome. You want to cultivate that kind of environment in the real interview too. Like take it from being overly formal and strict and have this sense of ownership of it. Even incorporate your humor, even just make it more casual, but still technical enough that the interviewer that you are working with also feels, okay, I like this dude. But they also respect your technical intellect. You clearly have the ability to nudge interviews in that direction. So do that. Like I want to call it out explicitly. I know it's not exactly technical feedback, but I want to note that because I think it really stands out. It makes it easy to. It makes it easy to basically have you on the good side of an interview. So even if somebody is coming with a bad mood, you don't want them being in a bad mood running through this. Because a significant element of the interview is how well your interview feels. So keep that in, keep that going. Good communication, really good vibes. Overall, I felt like the pace that we went with was pretty good. Like I think your pace from the problem starting to proposing a solution. Although you jump to the optimal solution even with the first problem quite fast, which is awesome. The only thing I'll point out is sometimes I feel like we might get lost perfecting our pseudocode. Which mind you, for any generic interview, I'll tell my candidates totally fine. But the problem with meta is they're going to be strict with time. Like they're really, really going to be strict. I believe they set the interviews to 50 minutes and your interview will probably even tell you, okay, let's get started and then we can leave questions for the end. Now be very mindful to not get lost trying to get perfect pseudocode and you know, bias yourself towards getting as fast as possible to the optimal solution. Like Meta is probably one of the few companies where if you immediately propose the optimal solution, it will not backfire like they are fine with that. So if you know the optimal solution, just propose it then, you know, I like what you did with the complexity. Just knock that out of the way first and then immediately start coding. So you wanna, you wanna make sure that you knock out the first two questions as fast as possible. Because if you knock the first two out and your interview is satisfied, typically if you get a third one, you almost guaranteed that you pass the interview. In fact, if you solve the third one, that's where you're now bordering the strong, strong, higher kind of feedback. So make sure you speed through them and not get stuck trying to perfect the pseudocode, especially at the start. And then, yeah, I think the understanding of traversals was clearly demonstrated, which was awesome. I liked how once, you know, once you clarified this, it barely took you a minute to essentially get the spot on answer here and even fix one edge case which is the null, that's perfect. I'll encourage you to make sure you practice with these traversal questions because typically all the questions that, or most of the questions that you'll be asked about binary trees and trees overall might entail some sort of traversal. So just knock that out, feel like you have a good fundamental, so you will be able to understand when to use it. So just keep that in mind because they're very, very popular. You know, you told me yourself, you saw that the most popular question in the list was a vertical order traversal. So that, that's just, you know, there's no other way of putting it. Like Meta, ask this quite a lot. So anticipate at least one or two of this. But overall this would have been a pass for me. Like a strong pass, really. Like, I really like the overall vibe or the only bits to make sure you're mindful of. Don't get too lost with code, make sure you have functional solutions. And good thing with Meta is they don't expect your code to run, but at least do a run through, which you did in this case as we went through this. So just be mindful of that. But yeah, you don't have to get perfect code. So I really liked how you even decided, okay, I'm gonna get binary search from collections. Totally fine. I always tell my candidates, you don't have to implement everything from scratch, but in this case, binary search maybe was slightly important because it's, you know, it's a modified version, so some interviewers will tell you to code it. Typically, what I say is just add a ghost function there, and then if you have time, fill it in. But it's totally okay to say, yeah, I'll assume this exists for now, and this is how it will work. But for now, let's focus on the meat. And then as soon as you're done with that, go fill in the details of that function that you've left open.
Magnetic Buffalo: Okay.
The Legendary Avenger: Makes sense.
Magnetic Buffalo: Yeah. I am definitely going to run for my next call.
The Legendary Avenger: Totally fine. Yeah. But I hope you enjoyed this. And I did enjoy it, and it was really nice talking to you.
Magnetic Buffalo: Okay, likewise. I had a great time.
The Legendary Avenger: Awesome. All right, thank you.
Magnetic Buffalo: All right, cheerio.

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.