NVIDIA Interview Question for Software Engineer Interns


Team: Driver Development
Country: United States
Interview Type: Phone Interview




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

public static Node mirrorTree(Node root) {
	if(root == null)  return;
	else {
		mirrorTree(root.left);
		mirrorTree(root.right);
		Node temp = root.left;
		root.left = root.right;
		root.right = temp;
	}
}

- Anonymous February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It says create a new BST

public static Node mirrorImage(Node root) {
		if(root == NULL) {
			return;
                }

		  Node n = new Node(root.data);
                n.left = mirrorImage(root.right);
                n.right = mirrorImage(root.left); 
                return n;
          }

- aks February 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do pre order traversal of the given tree and generate a list of elements. Now create a new BST and insert elements in the list one by one into the BST. This works only if the new tree to be obtained is a binary search tree.

- nivi1991 February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(root)
	{
		mirror = newnode(root->value);
		mirror->right = mirror_image(root->left,mirror->right);
		mirror->left = mirror_image(root->right,mirror->left);
	}
	return mirror;

i think ,it should work.
please comment guys.

- amit February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do Breadth First Search on Tree, Insert the node in the same order in other tree.

- RG February 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node{
	int data;
	struct node* left;
	struct node* right;
};
void mirror(struct node* node){
	if(node==NULL)
		return;
	else{
		struct node* temp;
		//do the subtrees
		mirror(node->left);
		mirror(node->right);
		//swap
		temp=node->left;
		node->left=node->right;
		node->right=temp;
	}

}

- icodingc February 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First attempt with original BST initialization and a display function to check accuracy after mirorring.
The code does not create a new BST yet.

#include <stdlib.h>
#include <iostream> 
#include <stdio.h>
struct Node;
Node * mirrorTree(Node * root);
//*****Utils Functions declarations****
Node * newNode (int value);				 //create a new node
int max (int left_hand, int right_hand);	         //return the max value between two ints
void testDisplayTree (Node * root);			//display the whole tree
void testDisplayNode (Node * node);			//display a node and its two children
//**************************************

struct Node { 
	int value; 
	Node * left; 
	Node * right; 
};

int main (int argc, char ** argv) { 
//create the binary tree
	/*
		Tree 
		 1
		/ \
		2  3
	   /\  /\
	   4 5 6 7
	   \
	    9
	*/
	Node * root = new Node;
	root->value = 1;
	root->left  = newNode(2); 
        root->right = newNode(3); 
	root->left->left = newNode(4); 
	root->left->right = newNode(5); 
	root->left->left->right = newNode(9);
	root->right->left = newNode(6); 
	root->right->right = newNode(7); 
	//test the layout of tree
	testDisplayTree (root);
	mirrorTree(root);
	printf ("After Mirror\n");
	//check if values where swapped
	testDisplayTree (root);
}

Node * mirrorTree(Node * root) { 
	if (root == NULL) { 
		return NULL;
	}
	if (root->left == NULL && root->right == NULL) {
		return root;
	}
	Node * left = mirrorTree (root->left);
	Node * right =  mirrorTree (root->right); 
	root->left = right; 
	root->right = left;
	return root;
}

 /********Utils functions *******/
//add a new node
Node * newNode (int value) {
	Node * node = new Node; 
	node->value = value; 
	node->left = NULL; 
	node->right = NULL; 
	return node;
}
//return the greatest int
int max (int left_hand, int right_hand) { 
	if (left_hand >= right_hand)
		return left_hand; 
	else 
		return right_hand;
}
//display a node and its children
void testDisplayNode(Node * node) { 
	printf ("node with value: %u, left child value: %d, right child value:%d\n", 
				node->value, 
				node->left !=NULL?node->left->value:-1,
				node->right!=NULL?node->right->value:-1);
}
//display the whole tree
void testDisplayTree (Node * root) { 
	if (root == NULL) { 
		return;
	}
		testDisplayNode(root);
		testDisplayTree (root->left); 
		testDisplayTree (root->right);
}

- nathaliekaligirwa October 14, 2014 | 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