## 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;
}
}

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;

}``````

Comment hidden because of low score. Click to expand.
0

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

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;
}``````

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)

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;
}
}

Comment hidden because of low score. Click to expand.
0

aren't we suppose to create a new tree ?

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;
}

}

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

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

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

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);
}
}``````

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;
}``````

Comment hidden because of low score. Click to expand.
0

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.

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;
}

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;
}

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

testing submit

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

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

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

int count = 10;

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

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

if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}

if(iret2)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
exit(EXIT_FAILURE);
}

/* 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.   */

exit(EXIT_SUCCESS);
}

void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
while(count > 0){
printf("Count --- %d, message--- %s\n",(10 - count), message);
count--;
}
}

void *print_message_function2( void *ptr )
{
char *message;
message = (char *) ptr;
while(count > 0){
printf("Count --- %d, message--- %s\n",(10 - count), message);
count--;
}
}``````

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

Using recursion is quite simple, just allocate a new node and recursively set the left and right children inverting them.
My C is a little rusty, but this is a complete implementation of the mirroring so you can fully understand the structure and debug it:

``````#include<stdio.h>

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

void print(Node* node)
{
if (!node) return;

print(node->left);

printf("%d ", node->value);

print(node->right);
}

Node* mirror(Node* node)
{
if (!node) return NULL;

Node* newNode = malloc(sizeof(Node));
newNode->value = node->value;
newNode->left = mirror(node->right);
newNode->right = mirror(node->left);

return newNode;
}

void freeTree(Node* node)
{
if (!node) return;

freeTree(node->left);
freeTree(node->right);

free(node);
}

int main(void)
{
Node left = {.value = 2, .left = NULL, .right = NULL};
Node right = {.value = 3, .left = NULL, .right = NULL};
Node root = {.value = 1, .left = &left, .right = &right};

print(&root);
printf("\n");

Node* mirrorTree = mirror(&root);

print(mirrorTree);
printf("\n");

freeTree(mirrorTree);

return 0;
}``````

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.

### 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.