Extract leaves from Binary Tree (Python)
Python Interview with an Amazon Engineer
Watch someone solve the extract leaves from tree problem in an interview with an Amazon engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.
Python interview with a FAANG engineer: Extract leaves from tree - YouTube
Interview Summary
Problem type
Extract leaves from tree
Interview question
- Given a tree, extract all of the leaves layer by layer
- Given a list of tuples representing a start and end time, determine how many meeting rooms are needed to schedule all of the meetings
Read more about the questions
Interview Feedback
Feedback about Epic Gargoyle (the interviewee)
Advance this person to the next round?
Yes
How were their technical skills?
3/4
How was their problem solving ability?
3/4
What about their communication ability?
3/4
Saying yes cuz of speed of solving problems, coding, thinking out loud, time/space analysis
action items provided during the interview
- reduced point for comm cuz candidate did not ask many questions and wasn't proactive above time,space and tests
- reduced point for problem solving cuz needed hints for problem 1
- reduced tech skill point cuz the approach used sorting for problem which increased the overall time, and I had to mention this to the interview to fix it.
If you called it out on your own, then that is better
Feedback about Rocket Samurai (the interviewer)
Would you want to work with this person?
Yes
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
Interview Transcript
Epic Gargoyle: Hello.
Rocket Samurai: Hello. Can you hear me?
Epic Gargoyle: Yes.
Rocket Samurai: Hi. So yeah, can you please briefly tell me about your current situation? How can I help you in this month?
Epic Gargoyle: Oh, yeah, sure, um, I'm going to have a virtual site with Amazon in, like, probably another 10 days. So probably, as far as I know, like, usually we will have one or two algorithm questions in a coding round. Probably also with some, like background introduction and behavior questions. So it could help me to, like, have a very standard mock interview for virtual onsite, something like that, if either to have behavioral questions and algorithm questions that will apply.
Rocket Samurai: Okay, yeah. So, anything, so if we just do algo questions, we can probably do two. And if we do like behavioral then maybe we'll not have enough time for two.
Epic Gargoyle: To do the other interviews, usually do interviews, usually, at the earlier questions first, or after the?
Rocket Samurai: Interviews usually usually have one or two behavioral questions and then algorithm.
Epic Gargoyle: I see I see. I mean, either way, we can start with our coding first. And if we have time, we do a behavioral question. Does that make sense?
Rocket Samurai: Okay, sounds good. So and what resources have you been using to prepare for these interviews?
Epic Gargoyle: Resources? Just get some practice on LeetCode.
Rocket Samurai: Okay. There's also this thing which might be useful, actually be useful to be aware of different patterns in coding problems. So like to pointers, sliding window to heaps? Topological sort, all of that. So yeah, all of that you can find like modified binary search. So it's good to be aware of, like, what patterns are out there and do a couple of examples of each pattern
Epic Gargoyle: That is very useful. Thanks.
...
Epic Gargoyle: Okay, I'm going to use Python to do the coding, the typing because okay, okay, we'll call this function collect and we will really treat the root on this tree into it and if we'll handle some corner cases like the trees actually announced value and we will just directly return empty list without reducer. Yep and we'll have hash table array for collecting everything okay cool. And? Okay so here we are going to the recursion function. Which means a tree just goes for it go for it right-hand side first. To the hash table for to the collection. Okay returned to level here using the shirt write out the tree no structure?
Rocket Samurai: No.
...
Rocket Samurai: Okay, look good. So let's stop here. So, overall, I think the interview went good. Especially like the last two problems, you had good intuition, I think the second problem you must have seen it before because you very quickly solved it right? So, whenever if you have seen the, maybe take some time to ask more questions, right? So in this case, you want to because you already know the problem, so you can ask more questions around like, what is the range of the values of any question given to you try to ask more questions in general, right? So like, what is the range of these meeting times? Can they be can there be negative times? And what is considered as overlap? Right? If, let's say, end time and start time of something, is the same? Is that considered an overlap? Or is that not considered an overlap? Right? So these questions are important to ask. That is what interviewers look for, because they want to see, are you gathering the requirements? Are you asking the right questions, before you even begin to solve the problem. And especially like, if you know the problem, then try to be a little slower, you don't want to rush quickly, because that will tell the interviewer that you already know the problem. And then that doesn't tell them anything about that will not tell them anything about problem solving skills, we want to slow down a little bit, try to organically move towards... Whenever you know a problem, try to come up with a naive approach first, and then build on that. That might slow you down and also tell the interviewer that you are organically at least thinking about it. Even if you know the problem, try to be somewhat a little discreet.
Epic Gargoyle: I see but that will need to do the record of like have a HashMap to record everything is there like that?
Rocket Samurai: Yes, we will have a HashMap to state when you do your DFS, right? You will record everything saying that, hey, please, parent is one two, spirit is one, sevens, parent is three, and so on. So when you go about top down, you can build that map. And that map is only using the extra size. And it is still better than taking hint from the interviewer right. So you even though you're spending a little bit extra space, it is not the end of the world. You don't necessarily have to come up with the most ideal solution. You just have to come up with a reasonable solution with your own thought process. So that is why this solution might be more intuitive because here you just reach the last set of leaves and then you work backwards.
Epic Gargoyle: Yep, makes sense.