Google Interview Question for Software Engineer / Developers


Team: Google Plus
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
2
of 2 vote

It would be a post order traversal with an additional feature that we'd pass in the root node to the traversal function. In the post-order

process node

step we just point the child node to the root node that we passes earlier.

I'm not sure if I understand the meaning of reversal correctly. In the current scene the reversed tree won't remain a tree, correct??

- Odi December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@odi ,,why can't be preorder ? can you provide the code for the same u r telling it will be helpful .

- nimish December 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm waiting for the original author to clarify the question. ts not clear as to how the o/p came out to be OutPut Head-> 8->3->6->9->NULL. If someone can explain this we can write the code ...

- Odi December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Can somebody explain the question?

- Anonymous December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I am assuming the struct of the Tree to be following:

struct Tree
{
  int data;
  int num_children;
  struct Tree** children; //Array of tree pointers to children
}

Tree **output;
static int count;

ReverseTernaryTree(Tree* Root)
{
    output = (Tree**)malloc(sizeof(Tree*)*100); // Assuming there are no more than  100 leaf nodes.
    int i;
    for(i=0;i<Root->num_children;i++)
    {
          Reverse(Root->children[i],Root);
    }
    
}


Reverse(Tree *node, Tree* prev)
{
   if(node.num_children == 0)
   {
       node->children[0] = prev;
       output[count] = node;
       count++;
       return;
   }
   else
   {
      int i=0;
      for(i=0; i<node->num_children; i++)
      {
          Tree* temp = node->children[i];
          node->children[i] = prev;
          Reverse(temp,node);
      }
   }
}

Explanation:

Output contains the list of all the pointers to leaf nodes which point to their parent. So, the entire tree is reversed in place.
Each child node is pointed to its parent and its child is also reversed to point to its parent.

- Anonymous December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

// there is bug so fixing it

struct Tree
{
  int data;
  int num_children;
  struct Tree** children; //Array of tree pointers to children
}


Tree **output;
static int count;


ReverseTernaryTree(Tree* Root)
{
    output = (Tree**)malloc(sizeof(Tree*)*100); // Assuming there are no more than  100 leaf nodes.
    int i;
    for(i=0;i<Root->num_children;i++)
    {
          Reverse(Root->children[i],Root);
    }
    
}


Reverse(Tree *node, Tree* prev)
{
   if (node == NULL)
        return;
   if(node->num_children == 0)
   {
       output[count] = node;
       count++;
       return;
   }
   else
   {
      int i=0;
      for(i=0; i<node->num_children; i++)
      {
          Tree* temp = node->children[i];
          Reverse(temp,node);
          output[count] = node->children[i];
          count++;
      }
      output[count] = node;
      count++;
   }
}

- googlybhai October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Let's make the problem more clear.

Q:Given a binary tree, reverse tree with following features:
1. For leaf nodes, their left pointers point to their parent, and their right pointers point to other leaves. You have to return a header pointer pointing to the first item in leaf list.
2. For none-leaf nodes, their left pointers point to their parent and their right pointer point to NULL.

Here is code:

typedef struct node {
  int data;
  struct node *left;
  struct node *right;
}Node;

Node *pre = NULL;
Node *head = NULL;

void reverseTree(Node *node, Node *parent){
  if (node == NULL)
    return;

  if (node->left == NULL && node->right == NULL) {
    if (pre == NULL) {
      head = node;
      pre = node;
    } else {
      pre->right = node;
      pre = node;
    }
    node->left = parent;
    return;
  }

  if (node->left != NULL) {
    reverseTree(node->left, node);
  }

  if (node->right != NULL) {
    reverseTree(node->right, node);
  }

  node->left = parent;
  node->right = NULL;
}

- mafish December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@mafish : the question is for n-ary tree and not for binary tree...

- vinodhian April 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What are the leaf nodes ? 89674??? what is Head-> 8->3->6->9->NULL then ?

- Kshitij December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

elementary reversal is to point the child nodes left pointers to the root. you can recurse this by first reversing the subtrees and then the root. working example as below

void reverse(Node* root)
  {
    
       if((root->left==NULL) && (root->right==NULL))
       {
                             cout<<"NULL reached";
                             List.add(root);
         return; }
      
                         
       if(root->left)
       reverse(root->left);
       
       if(root->right)
       reverse(root->right);
       
       cout<<"about to reverse node " << root->data<<endl;
       
       if(root->left)
       {
       root->left->left = root;
        root->left->right  = NULL;
        }
    
   if(root->right){
     root->right->right = root;
    root->right->right = NULL;
    } 
          
   }

- Kshitij December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@^^ its not binary tree ?

- nimish December 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Kshitij .leaf nodes are 8 , 3 , 6 , 9 , head no such things just return list containing leaf node .

- nimish December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can somebody explain the problem? can't catch it.

- Anonymous December 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

It seems to me that reversing a Tree is no different from reversing a LinkedList : visit a node, back up the reference to its children list, make it point to its parent and then recurse for each child passing them the current node as a parent.
I don't understand the leaf part though.

- Cardinal December 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1
/ | \
2 3 4
/ / \
5 6 7
/ \
8 9

Post-order... Also assuming that each node has only one parent (ie: can't be reached by multiple node in the original tree).

static List<Node> leafs = new List<Node>();
List<Node> Reverse(Node head, Node parent)
{
	if(head == null) return null;
	
	foreach(Node curr in head.Children)
	{
		Reverse(curr, head);
	}

	if(head.Children.Count == 0)
		leafs.Add(head);

	head.Children.Clear();

	if(parent != null)
		head.Children.Add(parent);
}

- Anonymous December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code will not compile but I think will serve the essence -

struct node
{
	//Data elements
	node* left;
	node* right;
}

void tree_reverse(node* root, node* parent, list<node>& leaves)
{
	if (root == null)
	{
		return;
	}
	else
	{
		if(root->right == null && root->left == null)
		{
			leaves.pushback(*root);
		}
		else
		{
			tree_reverse(root->left, root, list<node> leaves);
			tree_reverse(root->right, root, list<node> leaves);
		}
		root->left = parent; //The left pointer will be used
		root->right = null; // Right would be defunct
	}
}

- Odi January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Call ReverseTTree(root,null).

ReverseTTree(Node node,Node parent){

List list= new List();

int n=node.child.length;
int i=0;

if(n==0){
node.next=parent
node.prev=null;
list.add(node)
return list;
}

while( i<=n){
List li2 = ReverseTTree(node.child[i],node)
for(int j=0;j<li2.size();j++){
Node node1=(Node) li2.get(i);
node1.next=node;
node1.prev=null;
list.add(node1);
}
i++;
}
return list;
}

Please let me know for any bugs...

- sundi133 January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Gayle,
Can you please provide your thoughts?

- Stevey September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anyone with final, working and Good Solution?

- Andy2000 September 18, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More