I interviewed with Google. first of all, I found that google virtual meeting didn't work well for me as people couldn't hear me from their end. I am using macbook air, and airpods. I still don't know why this happen. Previously I used zoom for meeting and just directly used the macbook air speaker and microphone. It worked well on Zoom. This is my first time using google hangout and I was shocked when first find that macbook air microphone doesn't work on it. So it took me some time to figure out the settings and connect through airpods (they dont automatically connect on google hangout).
And during the interview, one interviewer asked this question:
Given a random list of tree nodes, traverse the tree in depth-first pre-order to print out node characters that:
a. One line per node;
b. each node prefixed with “_” according to its level.
The tree node is defined as:
class Node {
int id; // id is non zero
int p_id; // if p_id==0 then this is a root node.
char c;
}
e.g. Node A{5, 6, 'A'}, B{6, 0, "B"},C{4, 6, 'C'}
he explains that here we print "B" first without prefix, and then "A" with one "",and "C" with one "_"
I suggested using dictionary to save the information in list. And then print it out.
so I went like this:
def traversetree(arr):
dic={}
for i in range(len(arr)):
if arr[i].p_id not in dic:
dic[arr[i].p_id]=[[arr[i].id,arr[i].c]]
else:
dic[arr[i].p_id].append([arr[i].id,arr[i].c])
def recursive(pid,level):
for i in range(len(dic[pid])):
print level+dic[pid][i][1]
if dic[pid][i][0] in dic:
recursive(dic[pid][i][0],level+"_")
return
recursive(0,"")
return
the above was my general idea, but the interviewer kept saying that I was wrong (for the loop and print part in the recursive funtion). So can anyone tell me what does he mean? And he also asked me about the dictionary why I save the character information in the dictionary and he said I should use the tree directly. But isn't this input a list but not a tree? Was he asking me to construct a tree first?
If someone can point it out for me, really appreciate it.
Just added some follow up content below to reply on the first comment down here.
The question I pasted above is the content I see on the google doc. When I first see this I also found this question vague too. The interviewer clarified saying "let's say this is a binary tree". I noticed the "pre-order" key word too, but there is no information to tell if a node is left node or right node. Interviewer gave printing example as below:
B
_A
__G
___K
_C
__H
So I didn't focus on this left or right problem because I thought that maybe he just want to express in a way that the print like how directory and file name was printed.
And also since he said that this is binary tree in his explanation but not in the content of the question, I assume that he want to simplify this first to let me work it out as it is binary tree, and maybe later on follow up with question like what if this is a n-ary tree. So I just came up with loop through the children.
However, in the interview while I am writing this down and talking about it, for example , when I started and wrote to dictionary part, the interviewer asked "why you do this, and why you put this arr[i].c here". And when I got to the recursive part and try to write down this for loop, the interviewer kept saying that "no this is wrong, why you put this for loop here. You have a tree here, why don't you use it?". I was really confused during the interview and ask him: " so do you want me to construct the tree?" He didn't respond at his end. At one point, I felt it's very difficult for me to proceed with the interview.