NVIDIA Interview Question for Software Engineer Interns


Country: United States




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

void mirror(struct node* node) {
if (node==NULL) {
return;
}
else {
struct node* temp;

// do the subtrees
mirror(node->left);
mirror(node->right);

// swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;
}
}

- mail.anzar March 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Node* mirrorTree( Node *node )
{
	if (node == NULL)
		return NULL;

	Node *newNode = new Node();
	newNode->value = node->value;
	newNode->left = mirrorTree( node->right );
	newNode->right = mirrorTree(node->left);

	return newNode;

}

- supernova May 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the question is "...function in C..." This is not C, it is C++.

- William June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use pre-order traversal

#include <iostream>

using namespace std;

struct TreeNode {
        int val;
        TreeNode *left, *right;
        explicit TreeNode(int v):val(v),left(NULL),right(NULL){}
};

static void pre_order(TreeNode *root, TreeNode **node) {
        if (root == NULL) {
                return ;
        }

        *node = new TreeNode(root->val);
        pre_order(root->left, &((*node)->right));
        pre_order(root->right, &((*node)->left));
}

TreeNode *copy_mirrorly(TreeNode *root) {
        if (root == NULL) {
                return NULL;
        }

        TreeNode *node = NULL;
        pre_order(root, &node);
        return node;
}

static void pre_order_print_and_free(const TreeNode *root) {
        if (root == NULL) {
                return ;
        }

        cout << root->val << endl;
        TreeNode *left = root->left, *right = root->right;
        delete root;
        pre_order_print_and_free(left);
        pre_order_print_and_free(right);
}

int main() {
        TreeNode root(1), node1(2), node2(3), node3(4);
        root.left = &node1;
        root.right = &node2;
        node1.left = &node3;
        
        TreeNode *newL = copy_mirrorly(&root);
        
        pre_order_print_and_free(newL);
        return 0;
}

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

1) For each node in the tree count the no of nodes in the left subtree (k).
2) Store all elements in an array (A) and sort them
3) The root node of the BST will have the same number nodes in the left subtree as the one in the original tree. So use the count of root to find the element that will form the root of BST e.g. if left count is k then root of BST is A[k]
4) Recursively do this for the left and right subtree
Space complexity O(n)
Time complexity O(nlogn)

- kr.neerav February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirror(struct node* node) {
if (node==NULL) {
return;
}
else {
struct node* temp;

// do the subtrees
mirror(node->left);
mirror(node->right);

// swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;
}
}

- mail.anzar March 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

aren't we suppose to create a new tree ?

- Crooked February 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* mirrorBST(node *bst1 , node *bst2)
{
if(bst1 != NULL)
{
bst2 = (node*)malloc(sizeof(node));
bst2->val = bst1->val;
bst2->left = NULL;
bst2->right = NULL;
//printf("v = %d\n",root2->val );
bst2->right = mirrorBST(bst1->left,bst2->right);
bst2->left = mirrorBST(bst1->right,bst2->left);
return bst2;
}
else
{
return NULL;
}

}

- sigkill March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to get the mirror of any BAT simply do swap the root->left to root->right

- istiyak916 March 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That won't work as you need to interchange left/right child of all nodes in subtree of root->left and root->right.

- kkaushi August 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void MirrorTree(btre * root)
{
	btre* t;
	if(root==NULL) return;
	else 
	{
		t=root->l;
		root->l=root->r;
		root->r=t;

		MirrorTree(root->r);
		MirrorTree(root->l);
	}
}

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

typedef struct Node* Node;

Node mirror(Node root) {
	if (root == NULL) return NULL;
	Node result = malloc(sizeof(struct Node));
	result->left = mirror(root->right);
	result->right = mirror(root->left);
	result->val = root->val;
	return result;
}

- Vijay September 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void mirror(struct node *tree1)
{
struct node *temp;
if(tree1!=NULL)
{
temp=tree1->left;
tree1->left=tree1->right;
tree1->right=temp;
mirror(tree1->left);


mirror(tree1->right);
}
}
// where =>struct node
{
struct node *left;
int no;
struct node *right;
}
//
and (struct node *tree1) recevied the address of root node of tree.

- mithilesh kumar(tict kolkata) November 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// existing tree mirror image

struct tree* mirror(struct tree *ptr)
{
struct tree* temp;
if(ptr==NULL)
return NULL;

else
{
temp = ptr->left;
ptr->left = ptr->right;
ptr->right = temp;

mirror(ptr->left);
mirror(ptr->right);
}
return ptr;
}

- Shahid July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// existing tree mirror image

struct tree* mirror(struct tree *ptr)
{
struct tree* temp;
if(ptr==NULL)
return NULL;

else
{
temp = ptr->left;
ptr->left = ptr->right;
ptr->right = temp;

mirror(ptr->left);
mirror(ptr->right);
}

return ptr;
}

- Shahid July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

testing submit

- hayden.mcparlane February 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );
void *print_message_function2( void *ptr );

pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
pthread_cond_t  condition_var1	= PTHREAD_COND_INITIALIZER;

int count = 10;

int main()
{
	pthread_t thread1, thread2;
	const char *message1 = "Thread 1";
	const char *message2 = "Thread 2";
	int  iret1, iret2;

	/* Create independent threads each of which will execute function */

	iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
	if(iret1)
	{
		fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
		exit(EXIT_FAILURE);
	}

	iret2 = pthread_create( &thread2, NULL, print_message_function2, (void*) message2);
	if(iret2)
	{
		fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
		exit(EXIT_FAILURE);
	}

	printf("pthread_create() for thread 1 returns: %d\n",iret1);
	printf("pthread_create() for thread 2 returns: %d\n",iret2);

	/* Wait till threads are complete before main continues. Unless we  */
	/* wait we run the risk of executing an exit which will terminate   */
	/* the process and all threads before the threads have completed.   */

	pthread_join( thread1, NULL);
	pthread_join( thread2, NULL);

	exit(EXIT_SUCCESS);
}

void *print_message_function( void *ptr )
{
	char *message;
	message = (char *) ptr;
	while(count > 0){
		pthread_mutex_lock( &count_mutex );
		printf("Count --- %d, message--- %s\n",(10 - count), message);
		count--;
		pthread_cond_signal( &condition_var1 );
		pthread_cond_wait( &condition_var, &count_mutex );
		pthread_mutex_unlock( &count_mutex );
	}
	pthread_cond_signal( &condition_var1 );
}

void *print_message_function2( void *ptr )
{
	char *message;
	message = (char *) ptr;
	while(count > 0){
		pthread_mutex_lock( &count_mutex );
		printf("Count --- %d, message--- %s\n",(10 - count), message);
		count--;
		pthread_cond_signal( &condition_var );
		pthread_cond_wait( &condition_var1, &count_mutex );
		pthread_mutex_unlock( &count_mutex );
	}
	pthread_cond_signal( &condition_var );
}

- Ramesh September 29, 2016 | 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