Google- phone interview
Anonymous User
2456

2 part question
part 1:
given a list of tree(undirected graph) with vertices labeled from 1 to n, output a list using following step
res = empty list
while only 2 nodes exist

  • add the lowest numbered leaf's parent to res
  • remove that vertex

part2:
construct the graph when list (generated from above steps) is given

edges= 3-4, 3-1, 1-2, 1-5
    4-3-1-2
        5
remove 2 and add its parent 1
	4-3-1
		5
remove 4 and add itsparent 3
	  3-1
		5
remove 3 and add it's parent 1
		1
		5
		
res = [1,3,1]

Completely new question to me,
gave a quick solution for first part(using indegree & heap)
part 2 took almost halfhour, coded a solution with few edgecases left out
rejected

Comments (13)