Python Interview with a Google engineer.

Python Interview with a Google engineer

Interview Summary

Problem type
Delete Nodes and Return Forest

Interview question
You are given the root of a binary tree and an array of integers called to_delete.

Each node in the binary tree has:

Task:
Delete all nodes whose values are present in the to_delete array. After deleting these nodes, some children of deleted nodes may become new roots of separate subtrees.

Return a list of the roots of all remaining trees (the resulting forest). The order of roots in the output list does not matter.

Interview Feedback

Feedback about Redolent Unicorn (the interviewee)

What went well:

What could be improved:

Feedback about Samurai Gyroscope (the interviewer)

Very nice question

Interview Transcript

  1. Samurai Gyroscope: So the question looks like this. Imagine you are given a binary tree. Okay.
    Redolent Unicorn: Yeah.
    Samurai Gyroscope: And then it has some nodes. Does it look good to you?
    Redolent Unicorn: Yep, looks great.
    Samurai Gyroscope: Okay, and imagine, I mean by regular node representations, you have like node right, node left and node value right. And this whole tree can be represented as node1. And all of the nodes are connected under the hood. You will be given the root of this binary tree. And then you will be given something called to delete, which is going to be an array of integers.
    Redolent Unicorn: Just like random values for now, what I'm expecting from you.
    Samurai Gyroscope: So this will be your input argument. And then as an output, I'm expecting from you to go through your tree and then delete the nodes if their values are contained in that, to delete. So in this case, imagine we are deleting this 3 and 5.
    Redolent Unicorn: Okay, so if I delete three.
    Samurai Gyroscope: Yes.

  2. Redolent Unicorn: Okay.
    Samurai Gyroscope: So what I need to do is I can append the root. And if if root.value if root value is in the to delete set I will immediately remove the root because it's not part of the of the result.

Edge Cases

  1. If the tree provided to you is empty, what would be the expected result?
  2. If all nodes are deleted, what happens to the tree structure?
  3. If the nodes to delete are not present in the tree, do you get the original tree back?